与Selenium2混合的貂皮:遵循所有重定向 [英] Mink with Selenium2: follow all redirects

查看:57
本文介绍了与Selenium2混合的貂皮:遵循所有重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在执行某些断言之前强制Selenium2遵循所有重定向?

How to force Selenium2 to follow all redirects before doing some asserts?

  Scenario: Guest member can pay with card
    When I go to "/test"
    #test page redirects to "/auth" which then redirects to "/main"
    Then I should be redirected to "/main"

我认为我可以等一下:

  /**
   * @Then /^I should be redirected to "([^"]*)"$/
   */
  public function assertRedirect($url)
  {
    $this->getSession()->wait(10000);

    $this->assertPageAddress($url);
  }

问题在于,无论我等待多长时间,我总是最终进入"/auth"页面,而不是"/main"页面.

The problem is that however long I wait, I always end up on "/auth" page, not "/main".

更新:事实证明,这个问题是神话般的,硒没有做任何特别的事情,并且浏览器默认情况下会像通常那样进行重定向.就我而言,本应产生重定向的页面实际上正在发送200个响应.

UPDATE: It turns out the problem is mythical, selenium isn't doing anything special and browser is following redirects by default as it usually does. It my case the page that was supposed to produce redirect was actually sending 200 response.

推荐答案

我遇到了与您类似的情况.我设置了一个wait方法,该方法每秒对元素进行轮询,持续时间为x秒,以等待该元素变为可见状态.然后,我将Xpath传递给仅在最后一页或您的情况下为/main可用的元素.这是我在Java中使用的方法.

I have run into a situation similar to yours. I set up a wait method that polls for an element every second for x number of seconds waiting for the element to become visiable. I then pass an Xpath to an element only available on the last page, or /main in your case. Here is the method I use in java.

 public void waitForElement(WebDriver driver, final String xpath)
 {
     //Set up fluentWait to wait for 35 seconds polling every 1
     Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
         .withTimeout(35, TimeUnit.SECONDS)
         .pollingEvery(1, TimeUnit.SECONDS)
         .ignoring(NoSuchElementException.class);

     WebElement element;

     //Look for element, if not found start fluentWait
     try
     {
         element = driver.findElement(By.xpath(xpath));
     }
     catch (WebDriverException e)
     {
         logger.info("[getElementByXpath] Element not initially found. Starting fluentWait ["+xpath+"]");

         try
         {
             element = fluentWait.until(new Function<WebDriver, WebElement>() {
                 public WebElement apply(WebDriver d) {

                     return d.findElement(By.xpath(xpath));
                 }
             });
         }
         catch (WebDriverException f)
         {
             logger.info("[getElementByXpath] FluentWait findElement threw exception:\n\n" + f +"\n\n");

             throw new WebDriverException("Unable to find element ["+xpath+"]");
         }
     }

     //Once we've found the element wait for element to become visible
     fluentWait.until(ExpectedConditions.visibilityOf(element));
 }

您可能需要或不需要最后一次fluentWait来获得可见性,因为返回元素时,您将位于正确的/main页面上.

You may or may not need the last fluentWait for visibility as when the element is returned you will be on the correct /main page.

希望这会有所帮助.祝你好运!

Hope this helps. Good Luck!

这篇关于与Selenium2混合的貂皮:遵循所有重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆