I am trying to use watin to mimic login to live.com using c#. code is below.
IE myIE = new IE("http://login.live.com/");
myIE.TextField(Find.ByName("login")).TypeText("abc#abc.com");
myIE.TextField(Find.ByName("passwd")).TypeText("1234");
myIE.Button(Find.ByValue("Sign in")).Click();
However it always failed to find the textfield:
WatiN.Core.Exceptions.ElementNotFoundException: Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'login' at http://login.live.com/
The sample code in home page of http://watin.org/ works fine for www.google.com.
Did I miss something or is there anything special on http://login.live.com that prevents watin to work?
PS: I am running windows 7 64bit. VS 2008 with .net 3.5
You're hitting issues because the email field you're trying to type in is an HTML5 element.
Create the TextFieldExtended class as defined in this SO question: WatiN support for HTML5 tags
Then your code will be like the below:
ie.GoTo("http://login.live.com/");
ie.ElementOfType<TextFieldExtended>(Find.ByName("login")).TypeText("thisismyusername#here.com");
ie.TextField(Find.ByName("passwd")).TypeText("thisismypassword");
ie.Button(Find.ByValue("Sign in")).Click();
Tested on Watin2.1, IE9, Win7-64.
You may want to try this: I got it to work on my end:
ie.Div(Find.ByCustom("innertext","someone#example.com")).Click();
ie.TextField(Find.ById("i0116")).TypeText("hello");
ie.TextField(Find.ById("i0118")).Click();
ie.TextField(Find.ById("i0118")).TypeText("Hello!");
I recommend using this test Recorder. It will give you the elemnt names to use in your source:
http://www.codeproject.com/Articles/19180/WatiN-Test-Recorder
Edit:
I was also able to get this to work when finding by divID.
ie.Element(Find.ById("idDiv_PWD_UsernameExample")).Click()
Related
I wrote in Python a script which uses Selenium to auto-complete a form. It works with no issues.
I am very new to C# but I thought I would try and port it over so I can build a Windows executable to share it with a couple of non tech-savvy family members.
However, when I try what appears to be the same code, I get a timeout in C#.
As an example, I am trying to click a radio button:
HTML of radio button:
<input data-v-7af3e24c="" type="radio" id="condition-2" name="condition" class="govuk-radios__input" value="false">
Python (this works):
WebDriverWait(driver, max_wait).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '[id$=condition-2]'))).click()
However, when I try what I think is the same request in C#:
int elementLoadTime = 5; // Max 5 seconds for element to load
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, elementLoadTime));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("[id$=condition-2]"))).Click();
This produces:
Exception thrown: 'OpenQA.Selenium.WebDriverTimeoutException' in WebDriver.dll
The strange thing is, I am able to select the element in C# using the full XPath, so the element is clearly loading, and it strongly suggests the problem is with my CSS selector query.
// This works
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[1]/div[2]/main/div/div/form/fieldset/div[2]/div/div/div[2]/label"))).Click();
However, obviously that is a very brittle way of writing the code which will break the moment the site changes slightly.
I have also tried "[id$='condition-2']" and "[id$=\"condition-2\"]", to no avail.
Can anyone shed any light on what I am doing wrong?
Instead of using:
By.CssSelector("[id$=condition-2]")
Just use:
By.Id("condition-2")
If you want to use Xpath instead, do two things. First, forget that there's an option in your browser to 'Copy Xpath' - just purge it from your mind. Second, use this Xpath instead:
By.Xpath("//input[#id=\"condition-2\"]")
Hi I am new to Selenium Webdriver. I can successfully open a webpage and find elements on it.
In one case I have noted that there is a link on a page that becomes clickable after a while. In Firebug on the Script tab, I can see the code for the javascript that does the timer function.
But using Selenium Webdriver if I issue:
driver.PageSource
I cannot see the source code for the Javascript. Delaying for 30 seconds before requesting the source makes no difference. I have tried finding it with various By options using:
driver.FindElement
and so on, but it isnt there.
How does firebug manage to find and show the Javascript source code? Is there a way that I can coerce Selenium Webdriver to return all code referenced by the page?
Or is there a better approach?
Thanks for any advice!
EDIT---------------------
I tried the following in Firefox:
Dim Driver2 As IWebDriver = New Chrome.ChromeDriver
Driver2.Url = "http://mypage"
Dim js As IJavaScriptExecutor = TryCast(Driver2, IJavaScriptExecutor)
Dim title As String = DirectCast(js.ExecuteScript("return JSON.stringify(window)"), String)
and I got
Permission denied to access property 'toJSON'
I read that this wont work in firefox so I tried in Chrome, and got
Blocked a frame with origin "http://mypage" from accessing a
cross-origin frame
and from there no solutions because according to this its a security restriction, apparently you can't access an with Javascript
I'm starting to think Im a bit out of my depth here.
PageSource probably doesn't return an exact snapshot of the DOM & etc.
You can instead inspect javascript using driver.executeScript() but the burden of analyzing the return object may be discouraging.
Regardless - Here's a contrived example:
Object result = driver.executeScript("return JSON.stringify(window)");
System.out.println(result.toString());
If anyone can help me I'd appreciate it.
I'm working on a C# file in Visual Studio 2010 that I need to be able to test a website with multiple form pages, for the purpose of an example we'll refer to them all as 1.aspx, 2.aspx etc.
I've my code that fills out the first page (1.aspx) fine, and click the "continue" button to load the next page, but when it gets to 2.aspx it won't continue to fill out the form.
We'll say an element on the 2.aspx page is called "DOB". On trying to run from the start (I've all the pages form data in the one .cs file) I get an error like "DOB does not exist in the current context".
Anyone's insight into this would be really appreciated!
In all honesty, it sounds like you might be better off using the WatiN Framework. I have been writing automation with it for years and the way that it is implemented and its ease-of-use make it worth the slight learning curve.
Just to add a bit more to the answer; and yes, this is pseudo-code:
[Test]
public void Should_attach_to_browser()
{
ExecuteTest(browser =>
{
browser.GoTo(NewWindowUri);
browser.Link(Find.First()).Click();
var findBy = Find.ByTitle("New window");
var newWindow = Browswer.AttachTo(browser.GetType(), findBy);
newWindow.Close();
});
}
In the code above, note the Browser.AttachTo(browser.GetType(), findBy); method. Based on what I have understood of your question, the .AttachTo() method would work well since you would be able to take the focus off the current form and assign it to the next in your work/execution flow.
I just recently started looking into WatiN and was following the example from http://www.codeproject.com/KB/aspnet/WatiN.aspx. Unfortunately, I am running into an issue where it is claiming that a text field with the name "q" does not exist.
Here is my code:
[STAThread]
static void Main(string[] args)
{
IE ie = new IE();
ie.GoTo("http://www.google.com");
TextField ietxt = ie.TextField(Find.ByName("q"));
ietxt.TypeText("WatiN");
ie.Button(Find.ByValue("Google Search")).Click();
}
When it gets to the line ietxt.TypeText("WatiN") it throws the error:
"Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'q' at google.com/ (inner exception: Element wasn't available within 30 seconds.)"
It seems that everyone uses this example and it works fine for them. Also I went into the source code for google.com and found exactly where it states that "q" is indeed the name of the search text field.
<input name="q" title="Search" class="gsfi" id="lst-ib" ....>
Does anyone have any idea why I could be getting this error?
I found a fix for this! From further testing I came to the conclusion that the issue seemed to be coming from how IE 8 was configured on my machine. I tested on other machines with xp and IE8, and everything was working fine. Uninstalling and reinstalling IE8 has cleared up the issue.
I'm still not sure what was configured differently with my past version of IE8, so if anyone has any ideas of what could have caused this I'll be delighted to hear.
Thanks!
I'm trying to use the CodedUI Test feature of Visual Studio 2010.
I've got a problem while replaying the various actions for one of my html component. The Keyboard.SendKeys generated do not work (like if there was no input).
The code generated is :
// Type '{F4}{F4}{F2}titre{Enter}' in 'SaisieSD_DS' custom control
Keyboard.SendKeys(uISaisieSD_DSCustom, this.Params.UISaisieSD_DSCustomSendKeys, ModifierKeys.None);
If I replace the call to Keyboard.SendKeys by a call to System.Windows.Forms.SendKeys.SendWait, it does work.
I was thinking about a problem due to a loss of focus. However, if i do something like uISaisieSD_DSCustom.SetFocus(), it doesn't change the behavior.
Do you have any idea ?
thx.
Have you tried
uISaisieSD_DSCustom.WaitForReady()
Or one of the other waitfors?
Is it failing on this line? Or is it failing afterward due to this not working correctly?
You can also use the following to wait for all threads to complete before proceeding:
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
Keyboard.SendKeys(uISaisieSD_DSCustom, this.Params.UISaisieSD_DSCustomSendKeys, ModifierKeys.None);
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;
Just make sure you include the last line to turn it back to UIThreadOnly, or it will slow everything way down.
Visual Studio CodedUI Test searches for a control and sends those keys to it. In your case the control is 'uISaisieSD_DSCustom'.
You can try using:
Keyboard.SendKeys(this.Params.UISaisieSD_DSCustomSendKeys);
OR
Keyboard.SendKeys("{F4}{F4}{F2}titre{Enter}");
After typing the URL if we want to send the enter key then the below code works in Coded UI
Keyboard.SendKeys("{Enter}");