[英]Unable to find element after page refresh/redirect
我发现很难自动化基本的UI测试用例。 第一次使用硒。
我正在尝试为具有用户名和密码的用户登录模块的应用程序编写一些UI测试用例。 登录后,通常会出现两种情况:成功登录和密码错误。
成功登录后,应用程序URL会更改。 例如,从www.abc.com/AA到www.abc.com/BB。
在“密码错误”上,刷新一次页面,然后在同一页面上显示错误消息。
现在的问题是,我能够打开页面,通过Selenium输入U / N和Pswd,但是当页面重定向发生/刷新时,单击“登录”按钮后我遇到了问题。
Webdriver无法在页面上找到任何元素。 我已经检查过页面上没有IFrame。
我在.net控制台应用程序中使用C#Selenium Web驱动程序。 引用引用代码:
var options = new InternetExplorerOptions()
{
InitialBrowserUrl = URL,
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
};
var driver = new InternetExplorerDriver("www.abc.com/AA", options);
driver.Navigate();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var username = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("userName"));
});
username.SendKeys("User1");
var password = driver.FindElement(By.Id("userPwd"));
password.SendKeys("Password@123");
// Login button pressed in the next line
password.SendKeys(Keys.Enter);
//WAIT FOR RESULT
WebDriverWait waiter = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
// This below line Shows error "Unable to find Element..."
IWebElement categoryList = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("SAMPLE-DROPDOWN-ID"));
});
categoryList.SendKeys("1");
您可以尝试对条件URL进行条件循环检查,该URL等待X一段时间,直到它包含abc.com/BBB,然后在成功案例中检查Web元素的可见性,否则检查错误消息。 这也将使您更容易调试问题并减少测试出错的可能性。
编辑:这是您的查询的片段(请等待,直到URL更改)我对C#样式并不熟悉,但是在Java中,您可以应用相同的逻辑-
public static String start_URL = "http://example.com/";
public static WebDriver driver = null;
public static WebDriverWait wait = new WebDriverWait(driver, 10);
public static String prev_URL = null;
public static String curr_URL = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.navigate().to(start_URL);
prev_URL = driver.getCurrentUrl();
//do login operation - enter user name and password
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driverNew) {
return (driverNew.getCurrentUrl() != prev_URL);
}
};
wait.until(e);
curr_URL = driver.getCurrentUrl();
System.out.println(curr_URL);
}
问题在于Selenium不知道页面已刷新,它正在“上一页”页面中寻找新控件。
仅使用“发送键”并不能让Selenium判断发生了页面转换,您可以在文本区域中键入文本的段落。
如果有一个登录按钮,则应“单击”它,然后Selenium将期望重新加载页面。 否则,您将不得不强制Selenium手动重新加载页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.