"司机无法解决" - Selenium Java TestNG

时间:2017-03-14 18:32:22

标签: java selenium testng

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

但是我在行

中收到错误
  

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);

1 个答案:

答案 0 :(得分:3)

您需要进行以下更改。

原因是,driver不属于validuserpass测试范围。

在班级定义WebDriver driver,即在setup方法之前。

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


}