简体   繁体   中英

Selenium Webdriver and TestNG

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  
    }

}

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
}
}

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  
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM