【问题标题】:Get URL for opened tab Selenium/Java获取打开的选项卡 Selenium/Java 的 URL
【发布时间】:2018-11-09 11:38:19
【问题描述】:

我正在测试的内容: 转到网页,单击在新选项卡中打开的链接。确认新标签的 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);
       }

它采用的是原始 URL,而不是新标签页...

【问题讨论】:

    标签: java selenium automated-tests cucumber


    【解决方案1】:
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        System.out.println(String.format("handle: %s, url: %s", handle, driver.getWebDriver().getCurrentUrl()));
    } 
    

    【讨论】:

      【解决方案2】:

      在您调用click()新标签 中打开URL 的链接之前,您需要保存/存储父标签WindowHandle /em>:

      //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()next 从新标签中提取URL,你必须为numberOfWindowsToBe()2诱导WebDriverWait strong> 和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().*

      注意 B:当您将 currentURL 与示例 url 进行比较而不是使用 currentURL.equalsIgnoreCase("https://www.tourismpei.com/") 时,您可以使用 currentURL.contains("https://www.tourismpei.com/") linient 匹配。

      这里有关于Best way to keep track of Windows with Selenium in IE11?的详细讨论

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-07
        • 1970-01-01
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        • 2014-05-14
        相关资源
        最近更新 更多