简体   繁体   English

Selenium Webdriver:如何验证是否已加载所需的网页

[英]Selenium Webdriver: How to verify that the required webpage is loaded

I am using Selenium Webdriver to automate tests for a website. 我正在使用Selenium Webdriver自动化网站测试。 After every event I want to verify if the action was successful or not. 在每个事件之后,我都想验证操作是否成功。 What is the best way to do it. 最好的方法是什么。 Eg, How do I verify that correct webpage is loaded when a user clicks the SignIn button? 例如,当用户单击“登录”按钮时,如何验证是否加载了正确的网页? One thing that I am doing right now is to get the source of the resulting page and look for specific words in that page for confirmation. 我现在正在做的一件事是获取结果页面的来源,并在该页面中查找特定单词以进行确认。 If those words are there, I assume that the page is indeed loaded. 如果有这些单词,我认为页面确实已加载。 But I dont think it is a nice way to go about. 但是我不认为这是个好方法。

If you are using Selenium Webdriver, you can obviously use WebDriver.findElement(By locator) to check if a certain object that is only going to be in the page you are waiting for. 如果您使用的是Selenium Webdriver,则显然可以使用WebDriver.findElement(通过locator)检查某个对象是否仅会在您等待的页面中。 For Example, to check if login is successful, you can verify if "Logout" object is there in the page or not. 例如,要检查登录是否成功,可以验证页面中是否存在“注销”对象。

You don't have to take the source and check for required text. 您无需获取源代码并检查所需的文本。 If the id for Logout button is "Logout" below line works for you... 如果“注销”按钮的ID为“注销”,则下面的行适用于您...

assertTrue(driver.findElements(By.id("Logout").length > 0);

You should be testing general functionality, not that styling, etc. is correct. 您应该测试常规功能,而不是样式等正确。 The advantage of automatic tests over manual ones is that it's really easy to test things with a wide variety of different data and that it is fast to rerun them after changes. 与手动测试相比,自动测试的优势在于,使用多种不同的数据测试事物确实非常容易,并且更改后可以快速重新运行它们。 Generally you are making the assumption that pages are looking about correct and you just want to test your program logic in a multitude of different situations. 通常,您假设页面看上去正确,并且只想在多种不同情况下测试程序逻辑。

For instance, if you sign in, check that the user appears as signed in and that let's say front page is shown correctly (via some text that only appears in a certain place on front page). 例如,如果您登录,请检查用户是否显示为已登录状态,并假设正确显示了首页(通过一些仅显示在首页上特定位置的文本)。 Or if you have a multiple page form, where user is entering all sorts of data, check that the summary page lists everything correctly. 或者,如果您有一个多页表单,其中用户正在输入各种数据,请检查摘要页是否正确列出了所有内容。

Don't check grab the entire source and parse it, rather you ought to use something like: 不要检查获取整个源并进行解析,而是应该使用类似以下内容的东西:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import org.junit.*;

public class LoginTest extends WebDriverTest /* This would be your own superclass, from which you inherit the WebDriver instance. */ {
  private User user = new User();

  @Before
  public void setUp() {
    user.setName("Test Tommy");
    user.setPassword("foobar");
  }

  @Test
  public void userNameShouldBeShownAfterLogin() {
    // Go to your page and do the login, then wait for page load.

    String userNameOnPage = driver.findElement(By.id("usernameAfterLogin"));
    assertThat(userNameOnPage, is(equalTo(user.getName())));
  }
}

Page objects design pattern would be an ideal candidate for your requirement. 页面对象设计模式将是满足您需求的理想选择。 It all gives other benefits. 这一切都带来了其他好处。 Refer this link https://code.google.com/p/selenium/wiki/PageObjects 引用此链接https://code.google.com/p/selenium/wiki/PageObjects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM