后续测试方法中的驱动程序对象未执行

时间:2017-03-20 11:45:35

标签: java testng selenium-chromedriver

public class One {

    public WebDriver driver;

    @Test 
    public void test1() {
        /*System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();*/

        driver.findElement(By.id("username")).sendKeys("abc@ccp.com");
        driver.findElement(By.id("password")).sendKeys("password!1");
        System.out.println("im in first test case from demoTwo Class");
    }

    @BeforeMethod 
    public void test() {
        System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("https://app.anywebsite.com");
        System.out.println("im in first test case from demoONE Class");
    }

    @AfterMethod
    public void afterMethod() {

        // Close the driver
        driver.quit();
    }
}
  1. 如何将驱动程序对象传递给后续测试方法?

  2. 网上的testng示例代码显示了这种结构,但没有执行。

1 个答案:

答案 0 :(得分:1)

您只需这样设置您的驱动程序:

@BeforeMethod 
public void test() {
    System.setProperty("webdriver.chrome.driver", "Y:\\chromedriver.exe");
    this.driver = new ChromeDriver();

    driver.get("https://app.anywebsite.com");
    System.out.println("im in first test case from demoONE Class");
}