How can you avoid redundant call while waiting for element visibility - selenium

I currently have a test script using WebdriverIO that clicks on a button and waits for a popup to display. The purpose of the test is to make sure the element is displayed after a click, but since it is asynchronous I must wait till it is displayed.
But if I wait until the element is displayed my assertion will always pass if waitForVisible is successful. If it is not successful it will always throw an exception on the waitForVisible command and the assertion will never execute.
Here is an example of the code:
browser.click(btnElement)
browser.waitForVisible(popupElement)
expect(browser.isVisible(popupElement)).to.be.true
Is there a different way to tell the expect statement to retry/wait until the element is visible so the assertion is not useless?

You seem to be confusing the purpose of assertions, or misusing them, specifically in your example. You should only use assertions when you have the certainty that the thing (WebElement, text, attribute of an element, etc.) you want to verify contains the expected type of value (which you return into your assert, or expect statement for the final validation => the output is the desired one).
Assertions validate a static value, they don't poll the DOM. That's why you have tools like .waitForVisible(), waitForText(), or more importantly .waitUntil() (which offers more flexibility).
Granted, the expect assertion-style provided by ChaiJS might have a confusing lexicon (expect could be taken out of context: it is expecting of the WebElement to be visible). Just use them for validating outputs of different commands, not states of WebElements, or other dynamic/changing elements.
That being said, as a best-practice, you should always wrap your commands (e.g.: .click()) into a .waitUntil() block and assure the WebElement is ready for the action you're about to perform:
is it rendered in the DOM by the front-end logic? (use .isExisting())
is it visible in the DOM? (you *cannot click on elements which aren't visible in the viewport)
is it clickable? (or w/e other action you want to perform on it...)
In the end, a idiomatic action (in our case .click()) should look like this:
browser.waitUntil(() => {
browser.isExisting(locator);
}, timeout, "Oups! An error occured.\nReason: element ('" + locator + "') does not exist");
browser.waitUntil(() => {
browser.isVisible(locator);
}, timeout, "Oups! An error occured.\nReason: element ('" + locator + "') is not visible");
browser.waitUntil(() => {
browser.click(locator);
}, timeout, "Oups! An error occured.\nReason: element ('" + locator + "') could not be clicked");
You can wrap the whole thing into a custom-command and use it w/e you want. Say good-bye to flaky tests! :)
* Selenium is a user-centric web-automation tool, thus all actions are performed as if a user would (a user cannot click on elements that aren't visible, a user cannot click on multiple elements at the same time, or fill in an input field by typing a whole word, or a whole paragraph, etc.)

I don't know WebdriverIO but I would assume that it has something akin to a try-catch. Wrap your wait in a try-catch. If it succeeds, Assert.pass() or the equivalent. If it times out, an exception will be thrown so in your catch for the timeout put an Assert.fail() or the equivalent.
I've never used ChaiJS but I did take a look at the documentation and found a couple things you should be able to use.
assert.isOk(false, 'this will fail');
This is from the documentation and should be the equivalent to what I'm familiar with, Assert.Fail(). One would assume that a slight adjustment the following would be the equivalent to Assert.Pass(),
assert.isOk(true, 'this will pass?');
If that doesn't work for some reason, you can use the below from the documentation.
assert.isNotOk(false, 'this will pass');

Related

What can i use to interact with the search bar on this website using cypress

I am trying to complete a test case that involved opening propertypal.com, entering some values and validating the responses using the software cypress. I've never used cypress before but the company asking me to do this test want me to utilise it.
This is the website i'm testing https://www.propertypal.com/
I want to type bt6 into that text box, but I cant work out the correct locator to use. Everything I try either returns multiple elements or doesn't find anything.
Below are some of the things I tried with no success. The main things I have been honing in on are the placeholder text, the ID and element name.
I'm very new to this type of automation so any help would be amazing.
cy.get('query').type('bt6')
cy.get('input:first').should('have.attr', 'placeholder', 'Search Area, Address, Agent').click()
cy.get('search-form-textbox').type('bt6')
With this element
<input id="query"
type="text"
name="q"
class="search-form-textbox"
autocorrect="off"
spellcheck="false"
value=""
placeholder="Search Area, Address, Agent">
Using the id="query" should be best,
cy.get('input#query') // should only be one id "query" on the page
.type('bt6');
If there's multiple id's "query" and you want to flag it,
cy.get('input#query')
.then($els => {
expect($els.length).to.eq(1) // assert there's only one id found, otherwise fail
})
.type('bt6');
If there's multiple id's "query" and you don't really care, you can select the third which is the visible one.
cy.get('input#query')
.eq(2) // take the third, which is at center of the page
.type('bt6');
Taking the "nth" element found is always a bit fragile, but placeholder text is pretty good instead (provided the page isn't multi-lingual)
cy.get('input[placeholder="Search Area, Address, Agent"]') // easier with attribute in selector
.eq(1) // take the second, as the are two of these
.type('bt6');
Class is not so good, as can often be applied to multiple elements, but in this case it's pretty good because it's specific to the role,
cy.get('input.search-form-textbox') // prefix class with "."
.eq(2) // take the third, which is at center of the page
.type('bt6')
Web pages can often have multiple elements with the same selector, for example cy.get('input#query') has three elements with this id.
What happens is the developer creates a component, adds an id like <input id="query"> then adds the component in several places, so the page actually ends up with multiple ids of the same name.
When the page is complex with hidden sections, to find the element you want start by testing with a console.log
cy.get('input#query') // finds 3 elements
.then(console.log) // log them to dev tools
Open dev tools, click open the object printed and you can see the list of elements selected.
Now you can hover each element, and the corresponding element on the page is highlighted.
In this case the first two are hidden behind menu items, but the third is the one we need.
So now we can add an .eq(2) to select the third element
cy.get('input#query') // finds 3 elements
.eq(2) // take the third element
.type('bt6');
So when I ran the cypress Test for your website, I got an uncaught exception that is originating from your Application. This is something that should be analyzed and is possibly fixed.
If this is something that is not going to be fixed, then you can add the below in your test, so that cypress ignores the exception and doesn't throw an error.
Cypress.on('uncaught:exception', (err, runnable) => {
return false
})
Now I tried using the locator id(#query) to input the test, as it is always good to use id because it's unique, but unfortunately, it gave me an error as the element is not visible. Unfortunately adding {force: true} also didn't help me solve the issue.
cy.get('#query').type('bt6')
1.So, the locator that worked for me was .search-ctrl. So your Final Test will look like this:
cy.visit('https://www.propertypal.com/')
Cypress.on('uncaught:exception', (err, runnable) => {
return false
})
cy.get('.search-ctrl').type('bt6')
2.Now, if you want to globally handle the exception, then go to cypress/support/index.js and then write there:
Cypress.on('uncaught:exception', (err, runnable) => {
return false
})
And then in this case your test will just have:
cy.visit('https://www.propertypal.com/')
cy.get('.search-ctrl').type('bt6')

How to get the text from an element which disappears quickly using protractor

I need to get the text from element P but protractor keeps returning error
Code:
<div class = "ui-growl-message">
<span class = "ui-growl- title">Sucesso</span>
<p>cargo Cadastrado cm sucesso!</p>
</div>
I've tried this way:
const msgValidacao = element(by.css('ui-growl-message')).all(by.tagName('p')).first().getText().then(() => {
expect(msgValidacao).toContain('Cargo cadastrado com sucesso');
});
and the Error is:
Failed: No element found using locator: By(css selector,
ui-growl-message)
The problem is the element is a warning so it quickly disappears from the screen.
In addition to the css correction, you'll also want to employ some sort of wait strategy to anticipate the message and grab the content as close to the moment of the initial rendering as possible. Automation around very short-lived messages can be challenging due to intricate timing factors.
It might be not the real problem why it returns that element is not found. I thing that the selector is not good. If the element disappears quickly as You say sometimes the test will pass and sometimes it will fail. Try another selector and make sure that You have the correct one.
If you want to select first element use get(0) not first()
element(by.css('ui-growl-message')).all(by.tagName('p')).get(0)

Waiting function for CSS element in Capybara

There are waiting and non-waiting functions in Capybara. I am trying to use the first as much as possible.
Is there a way to use find (or one of its derivatives) to look for a CSS element that appears asynchronously?
Unfortunately, function page.has_css? is a non-waiting function.
Your claim that page.has_css? is a non-waiting function is not correct. page.has_css? will wait up to Capybara.default_max_wait_time seconds for an element to appear on the page and return true or false based on whether or not the element is on the page within that time. Therefore, given Capybara.default_max_wait_time = 5
if page.has_css?("div#my_div")
puts "on page"
else
puts "not on page"
end
will wait for up to 5 seconds. As soon as a div with id of "my_div" is visible on the page it will print "on page". If a matching div doesn't become visible within 5 seconds it will print "not on page". If you want it to wait longer for a specific call you can override default_max_wait_time by passing the :wait option
if page.has_css?("div#my_div", wait: 10) # will wait up to 10 seconds
...
If rather than wanting a boolean response you want to ensure the element exists you can do any of
page.assert_selector(:css, 'div#my_div') # :css is the default so can usually be omitted
page.assert_css('div#my_div') # minitest
expect(page).to have_css('div#my_div') # RSpec
which will wait for the element to appear as previously, but raise an exception rather than returning false if the element does not appear
If you need the actual element you can use find which will also wait. So
el = page.find(:css, 'div#my_div') # :css is the default and can usually be omitted
will wait for the element to appear and return the element when found. If the element is not found within default_max_wait_time seconds it will raise an exception.
Note: Basically, the only non-waiting methods are all and first and even those can be made to wait if you pass in any of the count options (:count, :minimum, :maximum, :between), although the elements returned from them would still be non-reloadable and can therefore have further behavior effects if used for scoping, etc. Even methods like click_link, click_button, etc will wait since they are implemented as find(...).click, etc

Selenium Webdriver - using isDisplayed() in If statement is not working

I am creating a script that involved searching for a record and then updating the record. On the search screen, the user has the option of viewing advanced search options. To toggle showing or hiding advanced search is controlled by one button.
<a title="Searches" href="javascript:expandFilters()"><img border="0" align="absmiddle" alt="Advanced" src="****MASKED URL****"></a>
The only difference between the properties of the search button when it is showing or hiding the advanced search is the img src:
When advanced search is hidden the IMG src ends with "/Styles/_Images/advanced_button.jpg", when advanced search is visible, the IMG src ends with "/Styles/_Images/basic_button.png"
When I open the page, sometimes the Advanced search options are showing, sometimes they aren't. The value that I want to search on appears in the Advanced section, so for my script to work I have added an IF statement.
<input type="text" value="" maxlength="30" size="30" name="guiSystemID">
The IF statement looks for the fields that I need to enter data into, and if the field does not exist then that would indicate that the Advanced options are not visible I need to click on the button to expand the search option.
I created the following IF statement.
if (!driver.findElement(By.name("guiSystemID")).isDisplayed()) {
driver.findElement(By.cssSelector("img[alt='Advanced']")).click();
}
When I run the script and the Advanced search is expanded then the script runs successfully. However, when I run the script and the Advanced search is not expanded, the script fails, advising me that it could not find the object "guiSystemID". This is frustrating because if it can't find it then I want the script to continue, entering into the True path of the IF statement.
Has anyone got any suggestions about how else I could assess if the field is appearing without having the script fail because it can't find the field.
Thanks in advance
Simon
I might be late in answering this, but it might help someone else looking for the same.
I recently faced a similar problem while working with isDisplayed(). My code was something like this
if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )
{
/**Do this*/
}
else
{
/**Do this*/
}
This code works pretty well when the element that isDisplayed is trying to find is present. But when the element is absent, it continues looking for that and hence throws an exception "NosuchElementFound". So there was no way that I could test the else part.
I figured out a way to work with this(Surround the {if, else} with try and catch block, say something like this.
public void deleteSubVar() throws Exception
{
try
{
if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )
{
/**when the element is found do this*/
}
}
catch(Exception e)
{
/**include the else part here*/
}
}
Hope this helps :)
I've had mixed results with .isDisplayed() in the past. Since there are various methods to hide an element on the DOM, I think it boils down to a flexibility issue with isDisplayed(). I tend to come up with my own solutions to this. I'll share a couple things I do, then make a recommendation for your scenario.
Unless I have something very specific, I tend to use a wrapper method that performs a number of checks for visibility. Here's the concept, I'll leave the actual implementation approach to you. For general examples here, just assume "locator" is your chosen method of location (CSS, XPath, Name, ID, etc).
The first, and easiest check to make is to see if the element is even present on the DOM. If it's not present, it certainly isn't visible.
boolean isPresent = driver.findElements(locator).size() > 0;
Then, if that returns true, I'll check the dimensions of the element:
Dimension d = driver.findElement(locator).getSize();
boolean isVisible = (d.getHeight() > 0 && d.getWidth() > 0);
Now, dimensions, at times, can return a false positive if the element does in fact have height and width greater than zero, but, for example, another element covers the target element, making it appear hidden on the page (at least, I've encountered this a few times in the past). So, as a final check (if the dimension check returns true), I look at the style attribute of the element (if one has been defined) and set the value of a boolean accordingly:
String elementStyle = driver.findElement(locator).getAttribute("style");
boolean isVisible = !(elementStyle.equals("display: none;") || elementStyle.equals("visibility: hidden;"));
These work for a majority of element visibility scenarios I encounter, but there are times where your front end dev does something different that needs to be handled on it's own.
An easy scenario is when there's a CSS class that defines element visibility. It could be named anything, so let's assume "hidden" to be what we need to look for. In this case, a simple check of the 'class' attribute should yield suitable results (if any of the above approaches fail to do so):
boolean isHidden = driver.findElement(locator).getAttribute("class").contains("hidden");
Now, for your particular situation, based on the information you've given above, I'd recommend setting a boolean value based on evaluation of the "src" attribute. This would be a similar approach to the CSS class check just above, but used in a slightly different context, since we know exactly what attribute changes between the two states. Note that this would only work in this fashion if there are two states of the element (Advanced and Basic, as you've noted). If there are more states, I'd look into setting an enum value or something of the like. So, assuming the element represents either Advanced or Basic:
boolean isAdvanced = driver.findElement(locator).getAttribute("src").contains("advanced_button.jpg");
From any of these approaches, once you have your boolean value, you can begin your if/then logic accordingly.
My apologies for being long winded with this, but hopefully it helps get you on the right path.
Use of Try Catch defies the very purpose of isdisplayed() used as If condition, one can write below code without using "if"
try{
driver.findElement(By.xpath(noRecordId)).isDisplayed();
//Put then statements here
}
Catch(Exception e)
{//put else statement here.}

Testing the validation of a text box using Selenium

I am trying to test a webpage using Selenium and NUnit. One of my test cases entails the validation of text boxes. Using Selenium and C#, I am able to retrieve the value entered in the text box. But when the validation of the text box fails, an error message is displayed next to the text box.
So, here are my questions:
1. How can I test if an error was raised due to validation failure.
2. Can I get the text of that error.
3. Or, am I way off the mark and what I am trying to do is not at all possible.
I have tried reading the value of the element, but it always seems to be an empty string.
Say, for example, I am trying to test the webpage https://edit.yahoo.com/registration . When I enter "**myname&&" in the First Name field, an error appears stating "Only letters, spaces, hyphens, and apostrophes are allowed". I want to be able to test that this error was raised.
Also, I noticed that when Selenium opens the webpage and enters an incorrect value in the text box, the error message does not get displayed next to this text box. Whereas, when I open the webpage myself and enter an incorrect text, the error message is displayed
Thanks!!
You will have to use thread.sleep, but in a better way. It's better to write a function like this (I am writing this in JAVA, you should be able to write it for C#). This method will wait for the specified number of seconds for the element to be visible. If the element is not visible even after the specified number of seconds, then the method will return false. If it becomes visible then the method will return true.
Alternatively, you can use an assertion instead of returning a false condition so that your test fails.
public boolean waitForErrorMessage(String elementToWaitFor, int waitTimeInSeconds)
{
int timeOut=0;
while(!selenium.isVisible(elementToWaitFor))
{
if(timeOut<waitTimeInSeconds){
#sleep for one second
Thread.Sleep(1000);
}
else {
return false;
}
timeOut=timeOut+1;
}
return true;
}