java - Selenium Webdriver 和 TestNG

标签 java selenium-webdriver testng

我正在使用 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/

相关文章:

java - 使用java swing更新sql上的数据

c# - Specflow:通过步骤参数转换传递 IWebElement

java - 为什么 Try-With-Resources 不会每次都创建一个新实例

java - 如何在testng监听器java中使用自定义类注释

java - 是否可以使用 Spring Data 和 MongoDB 的索引功能从子类中索引父类(super class)的公共(public)字段?

java - 能够使用sheet api链接google电子表格中的两个电子表格

java - 在 eclipse for selenium 程序中,错误-在类驱动程序中找不到 main 方法

javascript - Angular Protractor : Execute Scripts in Browser Context

java - JUnit - 预期的异常消息正则表达式

java.lang.NoClassDefFoundError Android Studio Unity