简体   繁体   中英

Using DataProvider by initializing WebDriver one time within TestNG

I'm trying to check login page control by using dataprovider but i don't want to initialize webdriver again and again for each username password control. Once i come into login page, checking all concerned scenarios on login page in single time without starting another driver seems more convenient to me but i couldn't figure it out. When running following code, data[0][0] and data[0][1] is being correctly checked but it gives no such element on Login method having second priority test annotation when being tried to be typed data[1][0] and data[1][1]. Probably, it causes because driver is not looking at that page on that time. How can I handle this issue?

error:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='q-input-wrapper email-input']//input[@class='q-input']"}

code:

    public class TestCaseFirst {
    
    public WebDriver driver;
    
    @BeforeTest
    public void Start() throws InterruptedException {
        
        WebDriverManager.chromedriver().setup();
        driver= new ChromeDriver();
        driver.get("https://www.faxzas.com/");
        driver.manage().window().maximize();
        Thread.sleep(2000);}
        
    @Test(priority=1)
    public void RoadtoLogin() throws InterruptedException {
        
        driver.findElement(By.xpath("//a[@title='Close']")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//div[@class='login-container']//span[@id='not-logged-in-container']")).click();;
        Thread.sleep(1000);
    }
    
    
    @Test(dataProvider="loginInfos", priority=2)
    public void Login(String mail, String password) throws InterruptedException {
        
        driver.findElement(By.xpath("//div[@class='q-input-wrapper email-input']//input[@class='q-input']")).sendKeys(mail);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//div[@class='q-input-wrapper']//input[@class='q-input']")).sendKeys(password);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//button[@type='submit']")).click();
        Thread.sleep(1000);
        String description = driver.findElement(By.xpath("//div[@id='error-box-wrapper']//span[@class='message']")).getText();
        System.out.println(description);
    }
    
    
    @DataProvider(name="loginInfos")
    public Object[][] getData(){
        
        Object[][] data = new Object[6][2];
        data[0][0]="blackkfredo@gmail.com"; 
        data[0][1]=""; 
        data[1][0]="blackkfredo@gmail.com";
        data[1][1]="443242"; 
        data[2][0]=""; 
        data[2][1]="1a2b3c4d";
        data[3][0]="";
        data[3][1]=""; 
        data[4][0]="blackkfredogmail.com"; 
        data[4][1]="1a2b3c4d"; 
        data[5][0]="blackkfredo@gmail.com"; 
        data[5][1]="1a2b3c4d"; 
        
        return data;
    }
}

You need to reset your page to the login page where you are expecting the element to be. Either put an @AfterMethod and go back to the page you are trying to test or put an @BeforeMethod for the same. You may even want to wrap up your find element calls and handle the exceptions by going back to the main page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM