java - 使用 Selenium Web Driver 和 Java 切换到第一个选项卡

标签 java selenium testing selenium-webdriver automation

使用带有java的selenium web驱动程序,我试图自动化一个功能,我点击一个链接,它会打开一个新选项卡。我想为第一个选项卡编写一个全局方法,在主测试类中调用该方法时会切换到第二个选项卡。断言完成后,我有一个单独的方法在第二个选项卡上执行断言。我想编写另一个全局方法,该方法在调用时关闭第二个选项卡并切换回第一个选项卡并恢复第一个窗口上的测试。

我编写了以下方法和测试:

主要测试:

@Test(enabled = true, dataProvider = "TestThis", description = "ClickThere", groups = {"hello" })

public void AttributeValidationOnHelpPage(TestData testData, String Id, String TableName) {

        menu.switch(Id);
        data = menu.clickData();
        data.selectNoneDateRange();
        TablePage = data.openTable(TableName);
        TablePage.clickAttTab();
        menuPage.clickOnHelpPage();

        // This method, when called will switch to second tab.
         helpPage = menu.switcToHelpPage();

        // After swicthing below is the assertions i want to do on the second tab.

        Assert.assertTrue(driver.getCurrentUrl().contains("Second page Url."));
        String actualValue = helpOnThisPage.getRefTableAttributes();
        Assert.assertEquals(actualValue, "Reference Table Attributes", "Reference Table Attributes Missing");

        // This method, will close the second tab and switches back to first tab and resumes the test.
        helpPage.switchBackTOMainApplication();

    }


    Global Method for Swicthing to second tab:

    public HelpPage switchToHelpPage() {
        String originalHandle = driver.getWindowHandle();
        try {
            TimeUnit.SECONDS.sleep(8);
        } catch (InterruptedException ignore) {
        }
        wait.until(ExpectedConditions.documentReady());
        wait.until(ExpectedConditions.isNotLoading());
        for (String handle : driver.getWindowHandles()) {
            if (!handle.equals(originalHandle)) {
                driver.switchTo().window(handle);
            }
        }
        return new HelpPage(driver);
    }


    Global Method for closing the second tab and swicthing back to first tab.

    public boolean switchBackTOMainApplication() {
        boolean isValid = false;
        String originalHandle = driver.getWindowHandle();
        for (String handle : driver.getWindowHandles()) {
            if (!handle.equals(originalHandle)) {
                driver.switchTo().window(handle);
            }
        }
        driver.close();
        driver.switchTo().window(originalHandle);
        return isValid;
    }

这是我面临的问题。当调用“helpPage.switchBackTOMainApplication()”方法时,它会关闭第一个选项卡而不是切换到它。我需要做任何修改吗,这将关闭第二个选项卡并继续第一个选项卡上的测试。

最佳答案

switchBackTOMainApplication() 中的变量“originalHandle”具有第二个选项卡的句柄,因为返回是使用第二个选项卡作为当前窗口执行的。代码是 应更改为:-

public boolean switchBackTOMainApplication() {
    boolean isValid = false;
    String handleToClose = driver.getWindowHandle();
    String mainHandle;
    for (String handle : driver.getWindowHandles()) {
        if (!handle.equals(handleToClose)) {
            mainHandle=handle;
        }
    }
    driver.close();
    driver.switchTo().window(mainHandle);
    return isValid;
}

关于java - 使用 Selenium Web Driver 和 Java 切换到第一个选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62070379/

相关文章:

javascript - Selenium 服务器错误 : Unable to create new service chromedriverservice

testing - Flutter widget测试中如何使用List的find.byType匹配

c++ - 验证功能

ruby - 使用 Selenium 清除浏览器缓存

java - getSerializable 根据应用程序的退出方式返回不同的类型

java - gluProject 将 3D 坐标转换为 2D 坐标不会正确转换 2D Y 坐标

java - 设置mysql进程之间的优先级

java - Spring集成入站 channel 适配器中的覆盖方法

java - 使用 Selenium WebDriver 从 Javascript 源中提取变量

java - 类继承和类转换