简体   繁体   English

通过在 TestNG 内初始化 WebDriver 一次来使用 DataProvider

[英]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.我正在尝试使用 dataprovider 检查登录页面控件,但我不想为每个用户名密码控件一次又一次地初始化 webdriver。 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].运行以下代码时,正在正确检查 data[0][0] 和 data[0][1],但在尝试键入 data[1][0 ] 和数据[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.要么将 @AfterMethod 和 go 放回您尝试测试的页面,要么将 @BeforeMethod 放回相同的页面。 You may even want to wrap up your find element calls and handle the exceptions by going back to the main page.您甚至可能想要结束您的 find 元素调用并返回主页面来处理异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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