Search code examples
selenium-webdrivertestng-eclipse

driver cannot be resolved when running using TestNG


  package bannerTstNG;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class BannerTestNG {
        
        
        @BeforeTest
        public void OpenTheSuperAdmin() throws InterruptedException {
            System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://ss-superadmin-staging.labaiik.net/");
            driver.manage().window().maximize();
            driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
            driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
            Thread.sleep(1000);
            driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
            Thread.sleep(6000);
        }
    
        @Test
        public void ClickOnBanner() {
            driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
        
        }   
    }
    
    

The function OpenTheSuperAdmin() is running but when ClickOnBanner() is executed, I get the following error: driver cannot be resolved.

Why is OpenTheSuperAdmin() being executed without any errors and the driver error not showing there?


Solution

  • You are instantiating the driver variable inside of the OpenTheSuperAdmin method so it is outside of the scope of the ClickOnBanner test. Please try the following:

        package bannerTstNG;
        
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.chrome.ChromeDriver;
        import org.testng.annotations.AfterTest;
        import org.testng.annotations.BeforeTest;
        import org.testng.annotations.Test;
        
        public class BannerTestNG {
    
            private WebDriver driver;        
            
            @BeforeTest
            public void OpenTheSuperAdmin() throws InterruptedException {
                System.setProperty("webdriver.chrome.driver","D:\\myselenium\\bannerTstNG\\driver\\chromedriver\\chromedriver.exe");
    
                driver = new ChromeDriver();
    
                driver.get("https://ss-superadmin-staging.labaiik.net/");
                driver.manage().window().maximize();
                driver.findElement(By.xpath("//input[@id='email']")).sendKeys("[email protected]");
                driver.findElement(By.xpath("//input[@id='password']")).sendKeys("admin");
                Thread.sleep(1000);
                driver.findElement(By.xpath("//button[contains(text(),'Login')]")).click();
                Thread.sleep(6000);
            }
        
            @Test
            public void ClickOnBanner() {
                driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/div[1]/div[3]/div[1]/ul[1]/li[4]/a[1]")).click
            
            }
        }
    

    By declaring the driver as a field in the class you will now be able to access it from any of the tests inside of the class.