Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

"driver cannot be resolved" - Selenium Java TestNG

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.

HOWEVER I am getting an error in line

UName = driver.findElement(By.name("login_user"));
It says it driver cannot be resolved.

Would appreciate help

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make the following changes.

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

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


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...