Selenium Webdriver and TestNG

核能气质少年 提交于 2019-12-12 02:36:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!