Java SeleniumWebDriver和TestNG

Java SeleniumWebDriver和TestNG,java,selenium-webdriver,testng,qa,Java,Selenium Webdriver,Testng,Qa,我正在用TESTNG在webdriver上运行代码。。。第一个测试工作得很好,但是在我尝试执行test2之后。。。driver.findelement以红色下划线,根本不执行。以前的driver.findelement是棕色的,但test2之后是蓝色的,有什么原因说明它不工作吗 @Test(priority=1) public void launchSandBoxTestingTestNG() throws InterruptedException{ // Import FireFox

我正在用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
}
}
我还将把“@Test(priority=1)”放在“public void launchSandBoxTestingTestNg”中,并在“launchSandBoxTestingTestNg”中声明webdriver,但在测试方法之外

public void launchSandBoxTestingTestNG() throws InterruptedException{

// Import FireFox Driver
WebDriver driver = new FirefoxDriver();


 @Test(priority=1)
 public void test1(){
// 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  
   }

}

谢谢,拉维终于成功了!欣赏help@user6401108没问题:)