Selenium 2 (WebDriver) 中 Selenium 1 (Selenium RC) 的 isTextPresent 等效项

发布于 2024-12-08 19:55:54 字数 102 浏览 0 评论 0原文

Selenium 2 (WebDriver) 中没有 isTextPresent

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

There's no isTextPresent in Selenium 2 (WebDriver)

What is the correct way to assert the existence of some text on a page with WebDriver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不即不离 2024-12-15 19:55:54

我通常会做以下事情:

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

assertTrue(driver.getPageSource().contains("sometext"));

I normally do something along the lines of:

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

assertTrue(driver.getPageSource().contains("sometext"));
抱猫软卧 2024-12-15 19:55:54

页面源代码包含 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 来缩小搜索树的范围。

Page source contains HTML tags which might break your search text and result in false negatives. I found this solution works much like Selenium RC's isTextPresent API.

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

doing getText and then contains does have a performance trade-off. You might want to narrow down search tree by using a more specific WebElement.

相权↑美人 2024-12-15 19:55:54

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

In 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

I know this is a bit old, but I found a good answer here: Selenium 2.0 Web Driver: implementation of isTextPresent

In Python, this looks like:

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
鹿! 2024-12-15 19:55:54

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

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

Or if you want to actually check the text content of a WebElement you could do something like:

assertEquals(getMyWebElement().getText(), "Expected text");
枯叶蝶 2024-12-15 19:55:54

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

Selenium2 Java Code for isTextPresent (Selenium IDE Code) in JUnit4

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"));
}
只为守护你 2024-12-15 19:55:54

在 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]*$"));

The following code using Java in WebDriver should work:

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]*$"));
柏拉图鍀咏恒 2024-12-15 19:55:54

我编写了以下方法:

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

上面的方法调用如下:

assertTrue(isTextPresent("some text"));

它工作得很好。

I have written the following method:

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

The above method is called as below:

assertTrue(isTextPresent("some text"));

It is working nicely.

梦年海沫深 2024-12-15 19:55:54

使用 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

Testing if text is present in Ruby (a beginners approach) using firefox as target browser.

1) You need of course to download and run selenium server jar file with something like:

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

2) You need to install ruby, and in its bin folder, run commands to install additional gems:

gem install selenium-webdriver
gem install test-unit

3) create a file test-it.rb containing:

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) run ruby:

ruby test-it.rb
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文