本文介绍了“驱动无法解析"- 硒 Java TestNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那里的任何 TestNG 用户.构建我的第一个测试.从下面的代码片段中,您可以看到我在 BeforeTest 上注释了设置 Chrome 浏览器的信息.然后我注释了一个应该启动 Chrome 浏览器的测试.

For any TestNG users out there. Building my first test. From the snippet below you can see I have annotated BeforeTest with the info to set up the Chrome browser. Then I annotate a Test which should launch the Chrome browser.

但是我在行中遇到错误

UName = driver.findElement(By.name("login_user"));
它说驱动程序无法解析.

希望得到帮助

public class FirstTestNGFile {
        @BeforeTest
        public void setup() {
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium3\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        // URL for ASK
         String baseUrl = "https://BLAH BLAH / ";
        // User and passwords
         String goodUser = "wayne";
         String goodPassword = "askTest17";
         String badUser = "foo";
         String badPassword = "badpass";
         driver.get(baseUrl);
        }


@Test
    public void validuserpass() {
        // ------------------------------------------------
        // Able to login with valid username and password
        // --------------------------------------------
        // launch browser and direct it to the Base URL
        //  Enter a valid name for username
        // Enter Text on Register Screen
        WebElement UName;
        UName = driver.findElement(By.name("login_user"));
        UName.sendKeys(goodUser);

推荐答案

您需要进行以下更改.

原因是,driver 不在 validuserpass 测试的范围内.

The reason being, driver is not in the scope of validuserpass test.

在类级别,即在 setup 方法之前定义 WebDriver 驱动程序.

Define WebDriver driver at class level i.e., before the setup method.

public class FirstTestNGFile {
   WebDriver driver;

   @BeforeTest
   public void setup() {
      driver = new ChromeDriver();
      //Add the remaining statements as it is

   }

   //Add your test methods as it is


}

这篇关于“驱动无法解析"- 硒 Java TestNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:55