5

What I'm tesintg: Go to webpage, click on a link which opens in a new tab. Confirm the URL for the new tab.

I'm doing this in Cucumber/Selenium/Java.

This is my Gherkin

Scenario Outline: The HomePage tourism box links to the correct page

Given I navigate to the HomePage in <language>
When I click on the visit Tourism PEI Button
Then I am taken to the Tourism PEI landing page URL in a new tab

Examples:
  | language |
  | English  |
  | French   |

Code I have for clicking the link:

 @When("^I click on the Visit\\s" + pageElements + "$")
public void iClickFOnTheVisitTourismPEIButton(String element)  {
    pageModel.pageObject(element).click();
    progressObj.wait(1);
}

And code I have tried to get the URL from the new tab. I'm struggling here. Everything I try opens a new tab, then the screen goes blank and says data; in the URL or it will open a new tab on top of the one that already does. I know I'm not really on the right track here. This is just the last thing I have tried.

@Then("^I am\\s*((?:not)?)\\staken to the Tourism PEI landing page URL in a new tab$")
public void iAmTakenToTheTourismPEILandingPageURLInANewTab() {

    WebDriver driver = new ChromeDriver();
    Actions action = new Actions(driver);
    action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
}

I also tried this

 String currentURL;

       currentURL = driver.getWebDriver().getCurrentUrl();
       if (currentURL.equalsIgnoreCase("https://www.tourismpei.com/")) {
           System.out.println("Matches" + currentURL);
       } else {
           System.out.println("Does not match" + currentURL);
       }

It's taking the original URL and not the new tab...

Sowa
  • 79
  • 2
  • 9

2 Answers2

9
for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
    System.out.println(String.format("handle: %s, url: %s", handle, driver.getWebDriver().getCurrentUrl()));
} 
lance-java
  • 25,497
  • 4
  • 59
  • 101
2

Before you invoke click() on the link which opens the URL in a New Tab you need to save/store the WindowHandle of the parent Tab:

//Declare as global variables
String parent_tab;
String child_tab;
// your other code work
@When("^I click on the Visit\\s" + pageElements + "$")
public void iClickFOnTheVisitTourismPEIButton(String element)  {
    parent_tab = driver.getWindowHandle();
    pageModel.pageObject(element).click();
}

Once you have invoked the click() next to extract the URL from the New Tab you have to induce WebDriverWait for both numberOfWindowsToBe() 2 and urlContains() as follows:

@Then("^I am\\s*((?:not)?)\\staken to the Tourism PEI landing page URL in a new tab$")
public void iAmTakenToTheTourismPEILandingPageURLInANewTab() {

    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    while(i1.hasNext())
    {
        child_tab = i1.next();
        if (!parent_tab.equalsIgnoreCase(child_tab))
        {
        driver.switchTo().window(child_tab);
        new WebDriverWait(driver, 10).until(ExpectedConditions.urlContains("tourismpei.com"));
        String currentURL;
        currentURL = driver.getCurrentUrl();
        if (currentURL.equalsIgnoreCase("https://www.tourismpei.com/")) {
            System.out.println("Matches" + currentURL);
        } else {
        System.out.println("Does not match" + currentURL);
            }
        }
    }
}

Note A : As you have used driver.getWebDriver() as in driver.getWebDriver().getCurrentUrl(); you may have to replace all the instances of driver.* with driver.getWebDriver().*

Note B : While you compare the currentURL with the sample url instead of using currentURL.equalsIgnoreCase("https://www.tourismpei.com/") you may use currentURL.contains("https://www.tourismpei.com/") for a linient match.

Here you will find a detailed discussion on Best way to keep track of Windows with Selenium in IE11?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352