WatiN : Textfields are not getting automated while running the code as
WatiN.Core.IE window = new WatiN.Core.IE();
// Frames
// Model
TextField txt_txtName = window.TextField(Find.ByName("txtName"));
TextField txt_txtPassword = window.TextField(Find.ByName("txtPassword"));
Button btn_btnLogin = window.Button(Find.ByName("btnLogin"));
// Code
window.GoTo("http://134.554.444.55/asdfgfghh/");
txt_txtName.TypeText("fghfjghm");
txt_txtPassword.TypeText("gfhgjfgh");
btn_btnLogin.Click();
}
only the window.GoTo("http://134.554.444.55/asdfgfghh/"); code works and the rest are doing nothing,
When I am using a catch block it throws exception as
Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'txtName' at "http://134.554.444.55/asdfgfghh/ (inner exception: Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.IHTML开发者_运维百科Element'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).)
My answer is very similar with Pavlo's one, but I must advice to use Page
which is a built-in suport for Model
as described by Pavlo.
Create a class MyPage
public class MyPage : WatiN.Core.Page
{
public TextField NameField
{
get { return Document.TextField(Find.ByName("txtName")); }
}
public TextField PasswordField
{
get { return Document.TextField(Find.ByName("txtPassword")); }
}
public TextField LoginButton
{
get { return Document.Button(Find.ByName("btnLogin")); }
}
}
Then you can just call .Page<MyClass>
method.
using(var browser = new IE("http://134.554.444.55/asdfgfghh/"))
{
var page = browser.Page<MyClass>();
page.NameField.TypeText("name field");
page.PasswordField.TypeText("password field");
page.LoginButton.Click();
}
When you call Button, TextField or whatever it does not create mapping it actually searches for control on page. And if the page is not opened yet than control does not exist.
You can create properties that will find control when you request it. So you define a particular model as class with appropriate properties.
public TextField txt_txtName
{
get
{
return window.TextField(Find.ByName("txtName"));
}
}
Added: If creating properties does not work for you, then use this:
var model = new
{
txt_txtName = new Func<TextField>(() => window.TextField(Find.ByName("txtName"))),
txt_txtPassword = new Func<TextField>(() => window.TextField(Find.ByName("txtPassword"))),
btn_btnLogin = new Func<Button>(() => window.Button(Find.ByName("btnLogin")))
};
window.GoTo("http://134.554.444.55/asdfgfghh/");
model.txt_txtName().TypeText("fghfjghm");
model.txt_txtPassword().TypeText("gfhgjfgh");
model.btn_btnLogin().Click();
精彩评论