繁体   English   中英

使用 Selenium Web 驱动程序和 Java 切换到第一个选项卡

[英]Switching to First tab using Selenium Web Driver with Java

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

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

主要测试:

@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”具有第二个选项卡的句柄,因为 go 返回是使用第二个选项卡作为当前 window 执行的。 代码应更改为:-

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;
}

当您单击链接时,您应该切换到打开的选项卡并进行验证

public void switchToNextTab() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(1));
    }

之后,您应该返回主选项卡

public void switchTomainTab() {
    ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
    driver.switchTo().window(tab.get(0));
}

如果您需要关闭打开的选项卡然后返回主选项卡,请使用此选项

public void closeTabAndReturn() {
        driver.close();
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(0));
    }

暂无
暂无

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

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