Playwright C# - wait for input text to be empty - c#

I'm writing a test using Playwright with C# bindings, and I came across a problem with waiting for element input to have no text.
Before "add" action, input fields look like this:
Article Number input has id="Number", and Name has id="Name" - just to be clear.
After "add" action, input fields are cleared of text:
it's a matter of split second for inputs to be cleared of text, but Playwright doesn't wait for it and starts typing before clearing inputs, which messes up my test.
I've tried to use:
await page.WaitForSelectorAsync("#Number >> text=");
await page.WaitForSelectorAsync("#Name>> text=");
but it didn't help out.
How to wait for text to be empty?

I think your selector is asking Playwright to find an Element with no text. Input Elements store their contents in their Value attribute, so actually always have no text!
You can use this method to check an Input Elements value against a page object, or this method against an IElementHandle.
Maybe have a utility method, something like:
string inputValue = "input";
DateTime timeout = DateTime.Now.AddSeconds(5);
while(timeout > DateTime.Now && !string.IsNullOrEmpty(inputValue)){
inputValue = await page.InputValueAsync("#Number");
}
This will wait for the Input Element to have an empty value.
There is some caveats: Text= does check the value attributes on Input elements of type button & submit, as explained here.

Just in case someone else is looking for an answer to the same problem, Page.WaitForFunctionAsync(expression, arg, options) looks like the ideal way to wait for an element to be empty. The nice thing compared to the before mentioned solution is that it requires less code, the waiting is non busy, and there is less communication between client and browser, because the polling (until ready or timeout) happens inside the browser itself.
You can pass it the JavaScript expression (which will be executed in the browser), for example textBox => textBox.value == ''
And in the second parameter you can pass it the element handle of the textBox.
(I didn't try this myself because I'm normally using PlayWright with NodeJS, but I think with these hints you'll get a long way.)

This may not have been available when the question was asked.
You can now do:
await Expect(page.Locator("#Number")).ToHaveValueAsync("");

Related

Selenium C# Element Not Found Taking a Long Time

I am using Selenium for C# to test a page.
Is there a way to quickly fail if the element is not found on the page?
I am experiencing if the HTML element is not found on the page the Selenium test a very long time and then eventually it fails. Recommendations on quickly failing if the element not found is appreciated!
return WebDriver.FindElement(By.Id(myTextBoxId)
You can try to change the Timeout wait time at the beginning of your test.
// In C# you can use
ChromeDriver driver = new ChromeDriver("Path to Driver");
driver.Manage().Timeouts().ImplicitlyWait(new Timespan(0,0,2));
This should now wait for 2 seconds for an element to appear before failing. You can set this value to anything you want.
The search for element should fail right away if you do not use any implicit wait or explicit wait. If you are doing that please remove them. And, if you are mixing implicit and explicit waits then that's even going to make it slower. On the other hand, if you expect the element not to exist and want to quickly check if the element exists or not and proceed, use findElements() and size() on the list. Something like the following:
List<WebElement> elements = driver.findElements(By.xpath("something"));
if(elements.size()>0){
//element exist
}else{
//does not exist
}

How to apply Dynamic wait in Ranorex?

I wants to apply dynamic wait in ranorex.
To open a webpage I used static wait like this :-
Host.Local.OpenBrowser("http://www.ranorex.com/Documentation/Ranorex/html/M_Ranorex_WebDocument_Navigate_2.htm",
"firefox.exe");
Delay.Seconds(15);
Please provide me a proper solution in details. Waiting for your humble reply.
The easiest way is use the wait for document loaded method. This allows you to set a timeout that is the maximum to wait, but will continue when the element completes it's load. Here is the documentation on it,
http://www.ranorex.com/Documentation/Ranorex/html/M_Ranorex_WebDocument_WaitForDocumentLoaded_1.htm
First of all, you should be more detailed about your issues. Atm you actually don't state any issue and don't even specify the reason for the timeout.
I don't actually see why you would need a timeout there. The next element to be interacted with in your tests will have it's own search timeouts. In my experience I haven't had a need or a reason to have a delay for the browser opening.
If you truelly need a dynamic delay there, here's what you actually should validate.
1) Either select an element that always exists on the webpage when you open the browser or
2) Select the next element to be interacted with and build the delay ontop of either of these 2
Let's say that we have a Input field that we need to add text to after the page has opened. The best idea would be do wait for that element to exists and then continue with the test case.
So, we wait for the element to exist (add the element to the repository):
repo.DomPart.InputElementInfo.WaitForExists(30000);
And then we can continue with the test functionality:
repo.DomPart.InputElement.InnerText = "Test";
What waitForExists does is it waits for 30 seconds (30000 ms) for the element to exists. It it possible to catch an exception from this and add error handleing if the element is not found.
The dynamic functionality has to be added by you. In ranorex at one point you will always run into a timeout. It might be a specified delay, it might be the timeout for a repo element, etc. The "dynamic" functionality is mostly yours to do.
If this is not the answer you were looking for, please speicify the reason for the delay and i'll try to answer your specific issue more accurately.

Questions regarding Appium

Please bear with me as I am relatively new to Appium. I am writing C# tests in Appium for my Android app. I am stuck finding answers to questions below.
1) How to check if a particular element exists? Is there any boolean property or function returning true or false? The methods driver.GetElementById, driver.GetElementByName etc. throw exceptions if element doesn't exists.
2) Suppose I want to write a test for login. The user enters username and password and hits login button. The requests goes to server and it checks whether username-password pair exists in database. Meanwhile the loading indicator (progress dialog in Android) is shown on screen. How shall make a test suspend it's execution until response comes from server assuming I don't want to use something like Thread.Sleep function?
3) Can I check whether textfield validation is failed on screen? A control with black background and white text is shown below textfield upon validation failure if we set validation for that textfield through setError function. Is there any way to check that validation has failed?
Anticipating answers. Thanks.
For the first 2 questions (This is what I do in java, definitely can be implemented in c#) -
1) Use the polling technique - In a loop check for the element return of the following
#param - By by , int time
driver.findElement(By by);
This must not be null or empty.
If within the threshhold time the element is not present then fail the test.
In appium mode - isVisible() will be same as the above, as an element not visible will not be present.
2) Check for the next activity to be awaited. Use the same polling technique to keep on comparing the current activity with the awaited activity, If the awaited activity does not start within the threshold time then fail the test.
#param int time, String awaitedActivity
1) Get the current activity.
2) Compare with the awaited activity.
3) If same then break the loop.
4) Else sleep for a second and then continue till the time is exhausted.

find out if IWebBrowser2.ExecWB print was cancelled in C#

I am using internet explorer to print a html document like this in C#:
ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER,
2, null);
This works fine, but I would like to know if the used pressed print in the dialog or cancel.
Is it possble to get this information without a ugly window hook hack ? something like a certain out parameter ?
This is not possible. I had this problem and ended up testing all possible values for the last output parameter, the output parameter is not used for this command.

How to detect Javascript pop-up notifications in WatiN?

I have a, what seems to be, rather common scenario I'm trying to work through.
I have a site that accepts input through two different text fields. If the input is malformed or invalid, I receive a Javascript pop-up notification.
I will not always receive one, but I should in the event of (like I said earlier) malformed data, or when a search result couldn't be found.
How can I detect this in WatiN?
A quick Google search produced results that show how to click through them, but I'm curious as to whether or not I can detect when I get one?
In case anyone is wondering, I'm using WatiN to do some screen scraping for me, rather than integration testing :)
Thanks in advance!
Ian
Here's what I came up with.
I read this question several times before I came up with the obvious solution..
Can I read JavaScript alert box with WatiN?
This is the code I came up with.. While it does force a delay of 3 seconds if the alert doesn't happen, it works perfectly for my scenario.
Hope someone else finds this useful..
frame.Button(Find.ByName("go")).ClickNoWait();
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
while (stopwatch.Elapsed.TotalMilliseconds < 3000d)
{
if (alertDialogHandler.Exists())
{
// Do whatever I want to do when there is an alert box.
alertDialogHandler.OKButton.Click();
break;
}
}

Categories

Resources