How to restart test on verify element present - selenium

Am new with automation, and currently trying to do something in Selenium IDE but can't seem to find the information on how to do it properly.
So I am running a test on a webpage, clicks and such, and I want if a certain id element is on the page to restart the test, and if not to finish remaining of the code.
Alternatively, this can also be done by verify element not present, but I believe it will slow down the test by searching for something that isn't on the page most of the time.
So the intended steps:
Run Test;
Commands 1, 2, 3... are running
Before Command 19, element id=btnSalir is checked if present on the page
If true, test restarts and goes on the loop until id=btnSalir is not present on the page
If false, test runs remaining code or specific code, whichever is easier.
As alternative steps:
Run Test;
Commands 1, 2, 3... are running
Before Command 19, element id=btnSiguiente is checked if present on the page
If false, test restarts and goes on the loop until id=btnSiguiente is present on the page
If true, test runs remaining code or specific code, whichever is easier.
Also is there a way to reduce the amount of time on these verify element present or not present be reduced?
I tried if, do, repeat if, but can't make it work, maybe someone with some experience can give me some pointers?
Selenium IDE step screenshot

Something like this might help:
if|selenium.isElementPresent("id=btnSalir")
echo|Do some thing
else
echo|Do some other thing
end
The code snippet in the IF statement uses an internal Selenium function to detect if the element is present. The true or false result will cause the correct branch of the if-else-end statement to be executed. Using a construct like this in a test in the IDE will let you do one thing if an element is present and a different thing if it is not present.
Note: This works if you run the test in the Selenium IDE, but will not work if you run the same test in the selenium Side Runner (the "selenium" object is not defined in the Side Runner).

Related

Wait required before Selenium-ChomeDriver/Chome can click element correctly (ChromeDriver v 77 & 78, Chome v 78)

As mentioned in the title, the following issue occurs since chrome was updated to v78. Before chrome v78 all tests worked just fine.
The behavior is highly nondeterministic, so please bear with me.
Meaning sometimes it just works out of a sudden, without any changes. Most of the clicks in the tests are working. Just some fail repeatedly (most of the time).
We are using selenium with the latest driver for chrome 78. The problem appeared first on driver version 77 with chrome 78 running.
Not the spec-flow tests nor the adapter-code has changed in any way, but after the Chrome update basically all tests failed.
Doing a Thread.Wait(1000) before EVERY CLICK seems to be the most reliable way to avoid the problem, and clicks the element as requested.
When im not waiting, the driver runs through the "Click", including the steps listed below, without actually clicking or throwing any error.
lookup of the element:
new WebDriverWait(this.webDriver, this.WaitTimeout).Until<IWebElement>(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(by))
waiting for the element to be clickable:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(by));
scrolling towards the element:
((IJavaScriptExecutor) this.webDriver).ExecuteScript("window.scrollTo(0, " + (object) (e.Location.Y - 250) + ")");
and performing a click and release action on the element:
action.Click(element).Perform()
i have also tried several other variants of mouse clicks including the one from the comment.
Since the process just works through all this, without actually throwing an error or warning of any kind, i have no way of checking if the click actually happened (other than checking the current URL, which would be a nightmare to maintain in the tests).
So the website is not updating and remains on the source page and the test will fail because the next test doesn't run on the expected page.
now the question:
Is there anything i can query for or wait for to see whether the driver or chrome can actually click that element (really) or to verify that it was actually clicked?
original issue report:
https://github.com/jsakamoto/nupkg-selenium-webdriver-chromedriver/issues/66

When invoking js.exec in Geb/Spock, the exec method is flagged as 'null'

I am creating a suite of tests (using Geb/Spock) for a web site. In one of them, the element I want to access is on the top of the page, so, to make sure that is visible, I want to scroll to the top of the page.
The command I am using is:
browser.js.exec('window.scrollTo(0, 0);')
or variations of it like
js.exec('window.scrollTo(0, 0);')
or other alternative like:
js.exec('window.scrollBy(0, -250);')
None of them makes the page scroll up, and when executing I get the following error (it is the only error, no other feedback). The error message using the other options listed above is identical (other than the command itself):
Condition not satisfied:
browser.js.exec('window.scrollTo(0, 0);')
| | |
| | null
| geb.js.JavascriptInterface#4019094f
geb.Browser#3dcac33e
at UserCreatesCompany.Go to Home Page and click on the log to
GitHub button as user User1(UserCreatesCompany.groovy:170)
I can not interpret the message that 'exec' is null. What exactly it means?
To make things more interesting, at the end of this script I am running the following cleanup procedure
js.exec('window.scrollTo(0, document.body.scrollHeight);')
DeleteButton.click()
$("button",'data-automation-id':"button-modal-yes").click()
}
and that works well: the page scrolls down. So, does not seem a problem about some missing library.
Any suggestion of what I may be doing wrong?
The version of the different components I am using is:
groovyVersion = '2.5.4'
gebVersion = '2.3'
seleniumVersion = '3.141.59'
chromeDriverVersion = '2.45'
First of all, you should not need to ever manually scroll the page to make elements visible - Selenium WebDriver which is underpinning Geb will do that for you automatically as soon as you start interacting (clicking, setting value, etc) with content.
Secondly, the failure you are getting is a failed assertion coming from a statement in an automatically asserted (then: or expect:) Spock block. It feels to me that you don't understand a concept which is core to Spock and therefore you should read about it in the manual first. It should make the failure you're getting clearer.
Thanks for the answer. Clearly: I was not fully aware of the different constrains the different blocks impose on what is executable or not. The manual is pretty clear once you have stumbled!
I am intrigued by your first assertion pointing that Selenium WebDriver will move to the element as soon as I interact with it. That was my understanding but it was not working. I made sure the element in question had a unique identifier, but still, it was not able to found it if the element had to be found by scrolling up. On the other hand it worked smoothly when locating the element WebDriver scrolled the page down.
Thanks again for the explanation. I have learn something new today!

Safari Extension multiple start script inclusion

I am building a simple Safari extension and sometimes the start script start.js specified is never included ans sometimes it is included many times like so :
Has it ever happened to you ?
This is probably due to iframes. The injected script is loaded in every iframe in the current tab. I don't think you can stop the script itself loading, but you can use if (window == window.top) within the script to ensure that code is only executed in the main page, not the contained iframes.

Selenium: intermittent "element not found" issues

Every now and then my selenium tests randomly fail with an "element X not found" error message. I do a simple select by ID, eg.
click('sideBar_queryButton')
When I use the dom inspector, the element is there, so I wonder why selenium doesn't find it sometimes. When I run the same test again, it works or it breaks again, there seems to be no reliable way of reproducing it. Even tests which are there for ages seem to randomly break and then magically work again. Inserting a few sleep statements sometimes helps but not reliably. So I wonder if I'm using it incorrectly. Has anyone else had these problems with selenium and if so, how did you fix them?
Edit: I found it to be much more reliable to put some test markers in my pages and wait for them to appear. If you use asynchronous operations which might create race conditions in your tests, inserting a test marker into your html after you finish the operation worked pretty well for me. E.g.
$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));
That way, you can do a simple "waitForTextPresent" to see if things worked out and its much more reliable than guessing the browser's loading state. The testmarker class needs to be formatted in a way that it is not visible to the user (e.g. font color == background color).
Thanks for all your comments. After some deeper digging on the net and
in our tests I finally found combining these statements instead of a
simple waitForPageToLoad to be the cure for our issues:
waitForPageToLoad('')
// wait until all ajax activity has ceased. That check's jQuery's $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// wait a second for all JS to properly initialize
pause(1000)
There is still a pause in there which is somewhat ugly, but it does
the trick.

Is there a way to make actions optional in Selenium IDE?

This is a bit of a newbie question, but... is there a way to make actions optional in Selenium IDE? I'll provide a use case.
In the app I'm testing, users see a "hey, you're agreeing to the ToS by logging on"-type modal window at the beginning of each session. They have to click OK to continue, and they don't see the window again until the next session.
Based on what I've seen so far, I need to have one test suite for the first test each day, and a second test suite for all the others. The second suite is exactly the same except that it doesn't have the "click okay to dismiss the initial modal window" step. Alternatively, I could just remember that my first run of the test each day will fail, and that I have to run the test again.
Both of those "solutions" seem unnecessarily awkward. Can I just make the click command optional?
Create a javascript file called user-extensions.js with the below code. Then go into the Selenium IDE Options dialog and select your user-extensions.js file, restart Selenium and you'll be able to choose TryClick which will work the same as Click but suppress any errors.
Selenium.prototype.doTryClick = function(locator) {
try {
return Selenium.prototype.doClick.call(this,locator);
} catch(err) { return null; }
};
Perhaps overdue, but for future searchers.
You could use the if and endIf statements within the IDE.
If you are using cookies to decide whether to hide the ToS dialog, you could check that a certain cookie is set and if so, skip the click.
I haven't used the selenium IDE much, but I think doing the check would be much easier if you are using a programming language. I am not sure how to do it in HTML tests.
If you are using HTML, you could have a look for Selenium IDE Flow Control and see if that can do what you need. I haven't used this myself, but if looks like it supports if statements. You could use verifyCookie to check if the cookie exists.
Hope that helps.
As aj.esler pointed it out Selenium ID Flow control is a good solution that has worked for me.
Here is the Firefox add on
I use the gotoif, here is an example about how you can use it. When skip value is 1 then it will go to the label=jump line and will not execute everything from gotoif like to label=jump .
Another extremely useful flow control add-on for the IDE is SelBlocks
It will give you the ability to use: if/else/for/foreach/while and even a way to read variables from an XML file.
Use http://wiki.openqa.org/display/SEL/flowControl addon.
Make something like this :
1.storeElementPresent | //button[#name="cookie_law_accept"] | cookie_law
2.goToIf | storedVars['cookie_law']!=true | end
3.click | //button[#name="cookie_law_accept"]
4.label | end
Explain:
1.If element is present it will be stored as a "cookie_law" with value "true"
2.If cookie_law is not "true" - go to label "end" - other way go to next step
3.Click to cookie accept button (only when itsenter code herepresent because it its not - you go to "end" label and you skip this command)
4.You go here if there is no cookie law button :)