简体   繁体   中英

Switching to First tab using Selenium Web Driver with Java

Using selenium web driver with java, i am trying to automate a functionality where i click on a link and it opens a new tab. I want to write a global method for first tab which when called in main test class switches to second tab. I have a separate method to do the assertions on second tab, after the assertions are complete. I want to write another global method which when called closed the second tab and switches back to the first one and resumes the tests on first window.

I have written the below methods and tests:

Main test:

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

Here is the problem i am facing. When "helpPage.switchBackTOMainApplication()" method is called its closing the first tab instead of switching to it. Is there any modification i need to do, which will close the second tab and continue the tests on first tab.

The variable "originalHandle" in switchBackTOMainApplication() is having the handle for the second tab as the go back was performed with second tab as current window. The code s hould be changed as:-

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

When you click to link you should switch to opened tab and do your validation

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

After that, you should return to your main tab

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

If you need to close opened tab and then return to the main tab use this

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

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