【问题标题】:Equivalent of isTextPresent of Selenium 1 (Selenium RC) in Selenium 2 (WebDriver)相当于 Selenium 2 (WebDriver) 中 Selenium 1 (Selenium RC) 的 isTextPresent
【发布时间】:2011-12-04 03:22:45
【问题描述】:

Selenium 2 (WebDriver) 中没有 isTextPresent

使用 WebDriver 在页面上断言某些文本存在的正确方法是什么?

【问题讨论】:

    标签: webdriver selenium-webdriver assertion


    【解决方案1】:

    我通常会做以下事情:

    assertEquals(driver.getPageSource().contains("sometext"), true);
    
    assertTrue(driver.getPageSource().contains("sometext"));
    

    【讨论】:

    【解决方案2】:

    页面源包含可能会破坏您的搜索文本并导致误报的 HTML 标记。我发现这个解决方案很像 Selenium RC 的 isTextPresent API。

    WebDriver driver = new FirefoxDriver(); //or some other driver
    driver.findElement(By.tagName("body")).getText().contains("Some text to search")
    

    执行 getText 然后包含确实有性能权衡。您可能希望使用更具体的 WebElement 来缩小搜索树的范围。

    【讨论】:

      【解决方案3】:

      我知道这有点老了,但我在这里找到了一个很好的答案:Selenium 2.0 Web Driver: implementation of isTextPresent

      在 Python 中,这看起来像:

      def is_text_present(self, text):
          try: el = self.driver.find_element_by_tag_name("body")
          except NoSuchElementException, e: return False
          return text in el.text
      

      【讨论】:

        【解决方案4】:

        或者,如果您想实际检查 WebElement 的文本内容,您可以执行以下操作:

        assertEquals(getMyWebElement().getText(), "Expected text");
        

        【讨论】:

          【解决方案5】:

          JUnit4 中 isTextPresent 的 Selenium2 Java 代码(Selenium IDE 代码)

          public boolean isTextPresent(String str)
          {
              WebElement bodyElement = driver.findElement(By.tagName("body"));
              return bodyElement.getText().contains(str);
          }
          
          @Test
          public void testText() throws Exception {
              assertTrue(isTextPresent("Some Text to search"));
          }
          

          【讨论】:

            【解决方案6】:

            以下在 WebDriver 中使用 Java 的代码应该可以工作:

            assertTrue(driver.getPageSource().contains("Welcome Ripon Al Wasim"));
            assertTrue(driver.findElement(By.id("widget_205_after_login")).getText().matches("^[\\s\\S]*Welcome ripon[\\s\\S]*$"));
            

            【讨论】:

              【解决方案7】:

              我写了如下方法:

              public boolean isTextPresent(String text){
                      try{
                          boolean b = driver.getPageSource().contains(text);
                          return b;
                      }
                      catch(Exception e){
                          return false;
                      }
                  }
              

              上述方法调用如下:

              assertTrue(isTextPresent("some text"));
              

              效果很好。

              【讨论】:

              • assertTrue(boolean) 和其他 assert 方法来自 JUnit?我假设这些不是默认的 java 包。
              • 你不认为“driver.getPageSource().contains(text)”会有性能问题。我认为它会搜索页面源的大量文本。如果这是查找特定文本的唯一方法,那么您如何证明时间流逝和代码性能是合理的。感谢您发布这么好的问题。
              • @MKod:我同意你的看法。当它搜索页面的整个文本时,它可能会导致性能问题。它需要找到一种替代方法。我猜 WebDriver 会一天天改进
              • MKod:我使用了 TestNG 的 assertTrue(boolean)。也可以用JUnit,没问题
              【解决方案8】:

              使用 firefox 作为目标浏览器测试 Ruby 中是否存在文本(一种初学者方法)。

              1) 您当然需要下载并运行 selenium 服务器 jar 文件,例如:

              java - jar C:\Users\wmj\Downloads\selenium-server-standalone-2.25.0.jar
              

              2) 您需要安装 ruby​​,并在其 bin 文件夹中运行命令以安装其他 gem:

              gem install selenium-webdriver
              gem install test-unit
              

              3) 创建一个文件 test-it.rb,其中包含:

              require "selenium-webdriver"
              require "test/unit"
              
              class TestIt < Test::Unit::TestCase
              
                  def setup
                      @driver = Selenium::WebDriver.for :firefox
                      @base_url = "http://www.yoursitehere.com"
                      @driver.manage.timeouts.implicit_wait = 30
                      @verification_errors = []
                      @wait = Selenium::WebDriver::Wait.new :timeout => 10
                  end
              
              
                  def teardown
                      @driver.quit
                      assert_equal [], @verification_errors
                  end
              
                  def element_present?(how, what)
                      @driver.find_element(how, what)
                      true
                      rescue Selenium::WebDriver::Error::NoSuchElementError
                      false
                  end
              
                  def verify(&blk)
                      yield
                      rescue Test::Unit::AssertionFailedError => ex
                      @verification_errors << ex
                  end
              
                  def test_simple
              
                      @driver.get(@base_url + "/")
                      # simulate a click on a span that is contained in a "a href" link 
                      @driver.find_element(:css, "#linkLogin > span").click
                      # we clear username textbox
                      @driver.find_element(:id, "UserName").clear
                      # we enter username
                      @driver.find_element(:id, "UserName").send_keys "bozo"
                      # we clear password
                      @driver.find_element(:id, "Password").clear
                      # we enter password
                      @driver.find_element(:id, "Password").send_keys "123456"
                      # we click on a button where its css is named as "btn"
                      @driver.find_element(:css, "input.btn").click
              
                      # you can wait for page to load, to check if text "My account" is present in body tag
                      assert_nothing_raised do
                          @wait.until { @driver.find_element(:tag_name=>"body").text.include? "My account" }
                      end
                      # or you can use direct assertion to check if text "My account" is present in body tag
                      assert(@driver.find_element(:tag_name => "body").text.include?("My account"),"My account text check!")
              
                      @driver.find_element(:css, "input.btn").click
                  end
              end
              

              4) 运行红宝石:

              ruby test-it.rb
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-07-17
                • 1970-01-01
                • 1970-01-01
                • 2012-03-12
                相关资源
                最近更新 更多