简体   繁体   English

验证Selenium WebDriver中的链接?

[英]Verify a link in Selenium WebDriver?

I'm having a web link which directs to the "/login.html" page. 我有一个直接链接到“ /login.html”页面的Web链接。 How can I verify whether the link works properly. 如何验证链接是否正常工作。 I already have this : 我已经有这个了:

WebDriver driver = new FirefoxDriver();    
driver.get("www.something.com/login.html");
String mesg = "Login Page";
Assert.assertEquals(mesg, driver.getTitle());

but I want a proper way to verify. 但我想要一种正确的方法来进行验证。

A common solution is to identify an element or some text that exists on the expected page, and which doesn't exist in any content the user would see if something unexpected happened. 常见的解决方案是识别预期页面上存在的元素或某些文本,如果发生意外情况,用户将不会看到的任何内容均不存在。 Then just assert on the presence of that content. 然后只需声明该内容的存在即可。

For example (this code uses the WebDriver Python API, but the pattern applies to any language): 例如(此代码使用WebDriver Python API,但该模式适用于任何语言):

success_text = "Welcome, user foo!"
assert success_text in self.driver.find_element_by_css_selector("p.welcome_msg").text

You can keep adding more assertions or keep it simple, depending on your preference. 您可以根据自己的喜好继续添加更多的断言或使其简单。

You can try and implement HTTP status response checker, currently it's long-awaited feature for a couple of years already. 您可以尝试实现HTTP状态响应检查器,目前它已经是人们期待已久的功能 ,已经使用了几年。

It will look something like: 它看起来像:

 HttpResponse response = new HttpRequestBuilder(HttpMethod.GET, linkUrl)
                    .execute(); 
//response assertion

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

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