问题
I'm running a code on webdriver with TESTNG... the first test works perfectly fine but after when I try executing test2 ... the driver.findelement gets underlined in red and doesn't execute at all. Previous driver.findelement was brown but after test2 is blue, any reason to why its not working?
@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
}
}
回答1:
The question is not very clear may be this might be the problem. You are creating an WebDriver object inside a function. Make WebDriver object global.
Example
public class test {
WebDriver driver = new FirefoxDriver();
public void test1(){
//test logic
}
public void test2(){
// test logic
}
}
回答2:
I would also put "@Test(priority=1)" inside the "public void launchSandBoxTestingTestNg" and declare the webdriver inside the "launchSandBoxTestingTestNG" but outside the test methods
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
}
}
来源:https://stackoverflow.com/questions/37838987/selenium-webdriver-and-testng