Selenium Webdriver 和 TestNG [英] Selenium Webdriver and TestNG

查看:33
本文介绍了Selenium Webdriver 和 TestNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TESTNG 在 webdriver 上运行代码......第一个测试工作得很好,但在我尝试执行 test2 之后......driver.findelement 用红色下划线标出,并且没有'根本不执行.以前的 driver.findelement 是棕色的,但在 test2 之后是蓝色的,有什么理由不工作?

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  
    }

}

推荐答案

问题不是很清楚可能是这可能是问题所在.您正在函数内创建一个 WebDriver 对象.将 WebDriver 对象设为全局.

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.

示例

public class test {
WebDriver driver = new FirefoxDriver();

public void test1(){
 //test logic
}

public void test2(){
 // test logic
}
}

这篇关于Selenium Webdriver 和 TestNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆