首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用testng实现selenium webdriver中的继承

使用testng实现selenium webdriver中的继承
EN

Stack Overflow用户
提问于 2014-05-21 19:53:21
回答 2查看 8.9K关注 0票数 1
代码语言:javascript
运行
复制
package user;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

public class Login {

    public WebDriver driver;

  @Test

  public void Signin() throws Exception   {

      driver.manage().window().maximize();
      Thread.sleep(5000);
      driver.navigate().refresh();
      driver.findElement(By.xpath("//div[@id='header']/div/div/ul/li[6]/a")).click();
      driver.findElement(By.id("Email")).sendKeys("test@gmail.com");
      driver.findElement(By.id("password")).sendKeys("123456");
      driver.findElement(By.xpath("//tr[5]/td[2]/input")).click();
      Thread.sleep(5000);
    }


  @BeforeTest

  public void BeforeTest() {

     System.setProperty("webdriver.chrome.driver", "D:\\lib\\chromedriver.exe");
     driver = new ChromeDriver();
         driver.get("http://test.com/");
  }

继承

代码语言:javascript
运行
复制
package user;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.testng.annotations.Test;


public class Addtocart extends Login {


    public WebDriver driver;

  @Test

  public void Cart() throws Exception {

      Thread.sleep(5000);
      driver.findElement(By.xpath("//div[2]/div/div/a")).click();
      driver.findElement(By.xpath("//a[contains(@href, '/Catalog/Featured')]")).click();
      driver.findElement(By.id("//div[4]/div/a/img")).click();
      driver.findElement(By.id("anchorAddToWishList")).click();*/
  }


 public static void main(String[] args) throws Exception{

      Addtocart ac = new Addtocart();
      ac.BeforeMethod();
          ac.Signin();
      ac.Cart();
  }
}

当我首先尝试调用超类方法时。子类方法首先调用。如何初始调用超类方法

EN

回答 2

Stack Overflow用户

发布于 2017-06-21 03:39:47

使用TestNG时,请尝试使用:

代码语言:javascript
运行
复制
@Test(priority=1) (assign the position to execute)
票数 0
EN

Stack Overflow用户

发布于 2020-02-14 13:26:50

您从未在您的超类中定义'BeforeMethod‘方法,只是一个观察值。

TestNG - Number 0具有最高优先级(它将首先执行),优先级基于给定的数字,即0比1具有最高优先级。1具有比2更高优先级,依此类推。

公共类TestNG_Priority_Annotations {

代码语言:javascript
运行
复制
@Test(priority=6)
public void c_method(){
System.out.println("I'm in method C");
}
@Test(priority=9)
public void b_method(){
System.out.println("I'm in method B");
}
@Test(priority=1)
public void a_method(){
System.out.println("I'm in method A");
}
@Test(priority=0)
public void e_method(){
System.out.println("I'm in method E");
}
@Test(priority=3)
public void d_method(){
System.out.println("I'm in method D");
}

}

输出将为:

我用的是方法E

我用的是方法A

我用的是方法D

我用的是方法C

我用的是方法B

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23782423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档