我正在使用 TESTNG 在 webdriver 上运行代码...第一个测试工作得很好,但是当我尝试执行 test2 后...driver.findelement 会以红色下划线显示,并且不会'根本不执行。以前的 driver.findelement 是棕色的,但在 test2 之后是蓝色的,有什么原因使它不起作用?
@Test(priority=1)
public void launchSandBoxTestingTestNG() throws InterruptedException{
// Import FireFox Driver
WebDriver driver = new FirefoxDriver();
// Open up Sandbox Page
driver.get("****");
// Enter Usename and Password
// User
driver.findElement(By.id("userId")).sendKeys("****");
Thread.sleep(3000);
// Password
driver.findElement(By.id("password")).sendKeys("****");
Thread.sleep(3000);
// Click Login Button
driver.findElement(By.id("loginButton")).click();
}
@Test(priority=2)
public void test2(){
driver.findElement(By.xpath("****")).click();
// When I try running this code above it underlines the find element in red
// When I run it on web driver the test2 syntax doesnt work
//It gives me an option of casting an argument but not sure what that means
}
}
最佳答案
问题不是很清楚,可能这就是问题所在。 您正在函数内创建一个 WebDriver 对象。 使 WebDriver 对象全局化。
示例
public class test {
WebDriver driver = new FirefoxDriver();
public void test1(){
//test logic
}
public void test2(){
// test logic
}
}
关于java - Selenium Webdriver 和 TestNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838987/