Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...