如何检查页面是否加载了正确的URL和选项卡标题 [英] How to check whether the page loaded with the correct url and tab title
问题描述
我写了一种加载页面导航链接的方法.该方法有效,但是当我添加代码以检查正确的URL和选项卡标题时,未执行我的测试.有时会发生这样的情况:对于循环快速单击未加载页面的那一侧,我不知道这是否是一个问题,但是我无法检查页面是否加载了正确的url或选项卡标题,或者问题是我为检查正确的网址或标签标题而编写的代码.
I wrote a method to load the page navigation links. The method works, but when I added code to check the correct URL and tab title my test is not performed. Sometimes it happens that for loop fast clicks on the pages the side that does not get loaded, I do not know whether it is a problem but I can not check whether a page loaded with the correct url or tab title, or the problem is the code that I wrote for check the correct url or tab title.
这是我的方法:
public void showNavigationLinks(){
Actions action = new Actions(driver);
String[] submenus = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};
for(int i=0;i<submenus.length;i++)
{
Assert.assertTrue(driver.getCurrentUrl().toLowerCase().contains(submenus[i]));
Assert.assertTrue(driver.getTitle().contains(submenus[i]));
WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
wait(2000);
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
wait(3000);
}
link_all_product.click();
}
这是我的错误:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 2140
Only local connections are allowed.
Jan 17, 2017 4:51:23 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jan 17, 2017 4:51:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at PageObject.ProductPage.showNavigationLinks(ProductPage.java:620)
推荐答案
这不是错误,您的声明失败了!
That is not an error, your Assertion failed!
您正在声明...toLowerCase().contains(submenus[i])
.但是,您submenus
中的每个项目都包含大写字符!
You are Asserting ...toLowerCase().contains(submenus[i])
. However, every single item in your submenus
contains uppercase characters!
您可以将断言更改为...toLowerCase().contains(submenus[i].toLowerCase())
之类的内容.
You could change your Assertion to something like ...toLowerCase().contains(submenus[i].toLowerCase())
.
下一个问题将是:您来自driver.getCurrentUrl()
的URL是否真的包含您的submenus
项目中的每个 ?我怀疑不会!将您的Assert
更改为:
The next problem is going to be: Does your URL, from driver.getCurrentUrl()
really contain every single one of your submenus
items? I would suspect not! Change your Assert
to something like:
Assert.assertTrue("checking if URL contains: " + submenus[i],
driver.getCurrentUrl().toLowerCase().contains(submenus[i].toLowerCase()));
这样,您可以准确地看到Assert
出现故障的时间和原因.
That way you can see exactly when and why your Assert
is failing.
这篇关于如何检查页面是否加载了正确的URL和选项卡标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!