Assert an element is not present in the dom using GEB - selenium

I am trying to verify a page based on who logs in. Certain users have more security and will see more items on the page. When a user with lower security logs in, they will not see options. I need to verify those options are not present on the page.
I have a page object for the option that displays based on user security
testObject {$("#test")}
I have tried using isDisplayed()
boolean hidden = testObject.isDisplayed()
assert !hidden
But I keep getting an error that says
geb.error.RequiredPageContentNotPresent: The required page content
geb.navigator.EmptyNavigator' is not present
The error message is what I want to verify. The object is not present, and I need to verify that is true and pass the test.

You want to use the required option for your content element.
If the page is dynamic, maybe you also want to wait for a while before you let Geb decide that the element is empty. Checks for non-existence of elements can be tricky, because they could just pass because a dynamic element has not finished loading yet.
testObject(required: false, wait: 2) { $("#test") }
In your Geb test you just do this (no helper method needed):
given:
def page = to MyPage
expect:
page.testObject.empty

Related

Selenium - when exactly the webdriver instance gets updated?

I'm using selenium to automate a task on a very dynamic website with pyhton.
For this reason certain HTML elements of the current loaded page may or may not be present at the moment of the request from my code.
How exactly the webdriver instance gets updated and receives the new data from the web page?
Is it constantly connected and receive the change in the HTML code instantly?
Or it first download a first verion of the page when driver.get() is called, and then updates it whenever a function such as .find_element_by_class_name() is called?
Q. Is it constantly connected and receives the change in the HTML code instantly?
Ans. Well, for each Selenium command, there will be an HTTP request that will send to the browser driver and then to the server and the command will get, A HTTP response will be returned and the command/commands will get executed based on the response.
Now let's say in a situation you do,
driver.get()
Basically, this is a selenium command.
It would fire an HTTP request stating to launch the URL provided. Based on the response (200-Ok or anything else), you would either see the WebPage getting loaded or an error message.
Same way in Sequence all the Selenium commands will get executed.
It's obvious that we need locators to locate web elements in the UI.
Once we have them, we can tightly couple them with
driver.find_element_by_***
methods.
Things to be noted down here
You need to learn/understand :
Implicit Waits.
Explicit Waits.
Fluent Waits.
Implicit Waits :
By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.
Basically what it means is, whenever you use drive.find_element it gonna look for implicit waits if you have defined one.
If you've not defined one implicit wait, then the default value is 0.
Explicit wait
They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.
FluentWait
FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Reference Link
Updated :
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
Locators (by priority from top):
ID
name
classname
linkText
partialLinkText
tagName
css selector
xpath
Web page is loaded by driver.get().
But the driver doesn't "know" what elements are existing there. It just opens, loads the web page.
To access any element, to check element presence etc. you will need to do that particularly per each specific element / elements using commands like .find_element_by_class_name() with a specific element locator.

How to check wether am in correct page after submitting the form using assertion selenium/TestNG/Java

This is my scenario "i am submitting the form and once I submit its navigate into page call WebTable.I want to confirm now am incorrect page after submitting the form"
how to check this by using assertion? please help selenium /Java /TestNG
In my experience, you have basically two options available to you.
Wait for the URL to contain what you expect using driver.getCurrentUrl()
Wait for a specific element that will only appear on the next page
If you don't have a class already built for performing retries, you can use FluentWait.
Once you click the form you wait for an element to show up using Webdriverwait.
You pull the current URL and check with value and assert it.
submitBtn.click();
WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("etc")));
expectedUrl = driver.getCurrentUrl();
Assert.assertTrue(expectedUrl.equals("something"));
Selenium provides a good practice for this type of validation, and here comes FluentWait:
FluentWait fluentWait = new FluentWait(webDriverBPO).withTimeout(Duration.ofSeconds(20)).pollingEvery(Duration.ofSeconds(1)).ignoring(NullPointerException.class);
All you have to do after your last action, is to wait until URL contains your expected value:
fluentWait.until(ExpectedConditions.urlContains("your URL or keyword from URL"));
You can try also to wait for any element from the new page, but the URL loads faster than the content, so you will save some time (as you don't need to perform any other validations).

Selenium IDE - Assert that JavaScript redirect worked after clicking Ajax button

I have a button that executes an Ajax request and then it successfully redirects to another page.
How do I assert that the redirected page was successfully reached?
I have a clickAndWait on the button. But after that..?
you can use verifyTextPresent command to verify a unique lable or text in the redirected page.by that way you can fix you have successfully reached the redirected page.
try like this
command : verifyTextPresent
Target : some unique text in the redirected page.
i think your problem can be fixed by this.
IDE has many assert commands. You can use any of the one and you can achieve the test scope(here the page is navigated or not).
Example:
command : assertTitle
Target : check the title of the page.
In the above he used verifyTextPresent it will check weather the text is present or not in the page and it will continue the next step. If you use assert commands it will proceed the next step when the assert step is passed. Otherwise it fails.
One thing you keep in mind, selenium won't wait for Ajax kind of loading, it will wait for the Page loading. So, you have to put Wait commands explicitly to finish the Ajax loading.
You can get moreassertions when you convert the selenese code into the preferred language and testing framework. You can see that option in the Option tab in the IDE.
For more info on AssertCommands in SIDE

Filling in hidden inputs with Behat

I am writing Behat tests and I need to change the value of a hidden input field
<input type="hidden" id="input_id" ..... />
I need to change the value of this input field, but I keep getting
Form field with id|name|label|value "input_id" not found
I have been using the step
$steps->And('I fill in "1" for "input_id"', $world);
Is there something special which needs to be done to modify hidden input fields?
Despite the fact that user can't fill hidden fields, there are some situations when this is desirable to be able to fill hidden field for testing (as usually rules have exceptions). You can use next step in your feature context class to fill hidden field by name:
/**
* #Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
*/
public function iFillHiddenFieldWith($field, $value)
{
$this->getSession()->getPage()->find('css',
'input[name="'.$field.'"]')->setValue($value);
}
Rev is right. If real user can can change input fields via javascript by clicking a button or link. try doing that. Fields that are not visible to user are also not visible to Mink also.
Or else what you can do is Call $session->executeScript($javascript) from your context with $javascript like
$javascript = "document.getElementById('input_id').value='abc'";
$this->getSession()->executeScript($javascript);
and check if that works
It's intended by design. Mink is user+browser emulator. It emulates everything, that real user can do in real browser. And user surely can't fill hidden fields on the page - he just don't see them.
Mink is not crawler, it's a browser emulator. The whole idea of Mink is to describe real user interactions through simple and clean API. If there's something, that user can't do through real browser - you can't do it with Mink.
(source: http://groups.google.com/group/behat/browse_thread/thread/f06d423c27754c4d)

Is there a way to get a QTP to fail a test if a particular element appears on a web page

I am trying to test a JSP based web application with QTP. On some of the pages the JSP is coded to return a particular div element, which will have an ID attribute, to the browser, only of the underlying model has a certain boolean flag set. I'd like to be able to develop a QTP test that fails if the div is present in the returned web page. However, the QTP documentation doesn't seem to have any details on how to do this.
The point is to detect if the condition applies and then explicitly fail the test.
If Browser("b").Page("p").WebElement("html tag:=div", "html id:=theId").Exist Then
''# Report failure
Reporter.ReportEvent micFail, "Element Exists", "It shouldn't"
''# if you also want to stop the test
ExitTest
End If
The default behavior for QTP is to write in 'Report' when a check point fails. i.e. if an element is not found on the page , automatically the QTP will write in the log report. In order to disable that , and to customize your report depending on your test scenarios, you can disable the report logging from the
beginning and write only in case you found an abnormal behavior.
Reporter.Filter = rfDisableAll
'check point validations
Reporter.Filter = rfEnableAll