获取已打开的标签Selenium / Java

时间:2018-05-30 14:48:56

标签: java selenium automated-tests cucumber

我在做什么: 转到网页,单击在新选项卡中打开的链接。确认新标签的URL。

我在Cucumber / Selenium / Java中这样做。

这是我的小黄瓜

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   |

我点击链接的代码:

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

代码我试图从新标签中获取URL。我在这里挣扎。我尝试的所有内容都会打开一个新选项卡,然后屏幕显示为空白并显示数据;在URL中,它将在已经执行的选项卡上打开一个新选项卡。我知道我在这里并没有真正走上正轨。这是我尝试过的最后一件事。

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

我也试过这个

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

它采用的是原始网址,而不是新标签...

2 个答案:

答案 0 :(得分:3)

for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
    System.out.println(String.format("handle: %s, url: %s", handle, driver.getWebDriver().getCurrentUrl()));
} 

答案 1 :(得分:0)

新标签中打开网址的链接上调用click()之前,您需要保存/存储父 WindowHandle 标签

//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();
}

一旦您调用了click()旁边的新标签提取网址,您就必须为numberOfWindowsToBe() WebDriverWait > 2 和urlContains()如下:

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

注意A :正如您在driver.getWebDriver()中使用driver.getWebDriver().getCurrentUrl();一样,您可能必须将driver.*的所有实例替换为driver.getWebDriver().* < / p>

注意B :在将 currentURL 与示例 url 进行比较而不是使用currentURL.equalsIgnoreCase("https://www.tourismpei.com/")时,您可以使用{{1对于一个临时匹配。

在这里,您将找到有关Best way to keep track of Windows with Selenium in IE11?

的详细讨论