我正在使用带有PageObject模式的PageFactory。
在其中一个网站(重jQuery)中,某些页面加载速度很慢,尤其是第一次调用页面时。我得到“元素未显示”或“NoSuchElementException”等。同样下载。
因此,当我执行myproperty.Click()时,我得到一个错误,我的FindsBy离开了Windows 我开始调用我的“WaitElementVisible”extensionMethod,然后返回一个元素,然后单击 这使我的财产属性毫无意义。我错过了明显的?
[FindsBy(How = How.Id, Using = "user_password")]
public IWebElement txtPassword { get; set; }
public void DoSomething()
{
txtPassword.Click // Error Element is not displayed" or "NoSuchElementException"
//So FindsBy pointless And I need todo
var myElement=myDriver.WaitElementVisible(by.Id( "user_password"),30));
myElement.Click()
}
问题:
构造函数BasePage应该实现“WaitForPageToLoad”类型 方法寻找要显示的元素? 这会解决问题吗?
PageInitElement是否只是初始化属性,如果没有找到,则将其设置为null。这是正确的吗?
我最好忘记“[FindsBy(等等)”并在方法中做所有事情吗?
是否可以编写自己的“FindsByWithWait”属性以及如何执行此操作并避免编写如下所示的extensionMethod:
示例代码:
//Ideally I would like to implement a FindsByWithWait that does below
public static IWebElement WaitElementVisible(this IWebDriver driver, By by, int timeout = 10)
{
return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until((drv) => {
try
{
var ele = drv.FindElement(by);
return ele.Displayed ? ele : null;
}
catch (StaleElementReferenceException){return null;}
catch (NotFoundException){return null;}
});
}
public abstract class BasePage
{
protected BasePage
{
WaitForPageToLoad();
}
protected void WaitForPageToLoad()
{
var wait = new WebDriverWait(myDriver, TimeSpan.FromSeconds(30));
try
{
wait.Until(p => p.Title == PageTitle);
}
catch (Exception e)
{
//log etc...
}
}
}
public class LoginPage : BasePage
{
[FindsBy(How = How.Id, Using = "user_login")]
public IWebElement txtUserName { get; set; }
[FindsBy(How = How.Id, Using = "user_password")]
public IWebElement txtPassword { get; set; }
[FindsBy(How = How.Name, Using = "commit")]
public IWebElement btnLogin { get; set; }
public void Logon(string userName, string password)
{
txtUserName.Clear();
txtUserName.SendKeys(userName);// sometimes will fail (specially site loaded for first time)
txtPassword.Clear();
txtPassword.SendKeys(password); //(fails specially site loaded for first time)
btnLogin.Click();//THIS MIGHT FAIL
}
}
I hope you can clarify and answer some of the questions above,that will help me greatly with my Selenium learning curve.
Thanks a lot
答案 0 :(得分:0)
PageFactory现在我发现了C#很麻烦,我最好创建By vars和pagefactory元素。例如:
import {MAIN_URL} from '../mainUrl'
export function fetchPets() {
return function(dispatch) {
console.log("THE URL IS", MAIN_URL)
axios.get(`${MAIN_URL}/tests`)
.then(response => {
dispatch({
type: FETCH_TESTS,
payload: response
});
})
.catch(() => {
console.log("Can't fetch the test examples");
});
}
}
您可以编写自己的等待方法。示例:WaitForElementsOnPage