Having issue finding element Fluentlenium - java

I am writing some automated tests using Fluentlenium and PhantomJS. I am having trouble accessing the id "#title". The test I have written is as follows:
#Test
public void testCreateButton() {
startAppWithCallback(new F.Callback<TestBrowser>() {
public void invoke(TestBrowser browser) throws InterruptedException {
CalendarPage calendarPage = browser.createPage(CalendarPage.class);
calendarPage.withDefaultUrl(BASE_URL);
calendarPage.go();
calendarPage.selectCreateButton();
calendarPage.typeTitle("Java Fundamentals");
browser.await().atMost(3, TimeUnit.SECONDS);
}
});
}
The test is running, and seems to be able to select the Create button, which should then open up a modal window, but for some reason it is having trouble seeing the id on this modal. The error message that I get is as follows:
org.openqa.selenium.NoSuchElementException: No element is displayed or enabled. Can't set a new value.
Is there something I am not doing when it comes to accessing the id on the modal window? Any help at all would be much appreciated.

Usually modal windows take some time to attach to the DOM of the page you are accessing. Though you have added 3 seconds to wait for the element to appear/ attach to the DOM but the time is not sufficient. I would not recommend to increase the timeout but would recommend to wait until for the element to appear and then move forward. for e.g. you could do following thing to wait for an element to appear on the page instead of waiting statically:
FluentWaitMatcher matcher = page.await().atMost(, TimeUnit.SECONDS).until(findPattern);

Related

Get Text from Auto Suggest Dropdown not working in Selenium [duplicate]

This question already has an answer here:
How to automate Google Home Page auto suggestion?
(1 answer)
Closed 3 years ago.
I am trying to verify if the selected value in an AutoSuggest Dropdown is correct using Selenium. But using the getText() doesn't return any results.
Below is my code
public class AutoSuggestDropdownsTest {
public static void main(String[]args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/resources/executables/chromedriver") ;
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("https://example.com/");
driver.findElement(By.xpath("//*[#class='stamp remove-sign']")).click();
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys("to");
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
driver.findElement(By.xpath("//input[#id='or-search']")).sendKeys(Keys.DOWN);
System.out.println(driver.findElement("//input[#id='or-search']")).getText());
System.out.println("hello");
driver.quit();
}
Usually autosuggestion results take some time to appear, so if you do the sendKeys(Keys.DOWN) immediately after sending the to it is very likely that no autosuggestions appeared yet, and this way the DOWN key has no effect. To check if this is the case (for development purpose of the test only) put a sleep 10s, and check if with the sleep the text you think should appear, actually appears.
if it does appear, then you should implement some kind of smart waiting for the results to appear.
if it doesn't appear, maybe there is another problem, in which case it would be very useful if you could post the html of the page after the autossugestions appeared.

Wait for page to be loaded with HtmlUnitDriver

I have several tests that run with Selenium and HtmlUnitDriver. Sometimes when I run them and want to click on an element or read text, the elements can not be found. Every time a Exception is thrown I save the Code of the Page for debugging purposes. But when I check the Code, the element is there and when I rerun the test everything works fine.
My guess is that the page was not completely loaded when I try to access the element.
So I would like to wait until Selenium has finished loading the page before I try to access elements.
I fond two ways to achieve it:
Execute Javascript (e.g. window.initComplete) and wait for the result to be true. The problem: In Selenium I have to have an instance of JavascriptExecuter but HtmlUnitDriver is not derived from that class and I cannot switch to FirefoxDriver (which implements the JavascriptExecuter interface) because we are running the tests headless.
Wait for the last element on the page to load
The problem with this approach is that the page is based on our framework and if that changes and suddenly there are different elements on the bottom of the page I have to adapt every test.
Any suggestions on how to approach the problem?
You can create a custom ExpectedCondition:
public static ExpectedCondition<Boolean> waitForLoad() {
return new ExpectedCondition<Boolean>() {
#Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
}

Can I extend the .click method of a WebElement?

Is there a way to extend the .click method of a WebElement?
I would like to add a few lines of code to it to accommodate some issues we have with an internal website.
I would like to add:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(pageElement));
Now I know that some of you will probably say just use implicit wait. I've thought about that but some of the pages I code against take 10-30 seconds to load. Some of the pages load very quickly but then the buttons that get displayed are conditional based on clicking other buttons and I get into a situation where I know the button should have loaded within 5 seconds. I'd rather not incur the 30 second wait on every button. This could happen literally hundreds of times and I don't want the script to take that long.
Is there a way to add an explicit wait to a click event?
Try FluentWait, This will check for element every 5 seconds.
Wait wait = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
There isn't really a need to override .click() and as #lauda has stated in the comments, this is not a good practice. You don't need to add a lot of code to wait for the element to be clickable so the claim that it will add thousands of lines of code is not true. You can easily accomplish this with a single line of code... so no extra lines of code. Having said that... you should focus less on how many lines of code it will add and consider:
How easy is it going to be to maintain?
Am I adding unnecessary complexity to my code base?
Is it going to be more readable?
...and so on...
A sample click method for a representative button
public void clickButtonX()
{
new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.elementToBeClickable(buttonXLocator)).click();
}
where you have these declared at the top of the class
private By buttonXLocator = By.id("buttonXId");
private int timeOutInSeconds = 10;
Having said that... I don't think this is the right approach. With the page object model, you should have each page class handle waiting for the page to finish loading... then you won't have to wait for the buttons to load before clicking them. In the case where a button is loaded dynamically, the action that triggers the dynamic loading should wait for the load to complete.
package sandbox;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import common.Functions;
public class _PageTemplate
{
private WebDriver driver;
private By dynamicPageLoadLocator = By.id("someId");
private By buttonXLocator = By.id("buttonXId");
private By dynamicLinkLocator = By.id("dynamicLinkId");
private By dynamicSectionElementLocator = By.id("dynamicSectionId");
public _PageTemplate(WebDriver webDriver) throws IllegalStateException
{
Functions.waitForPageLoad(driver);
// see if we're on the right page
if (!driver.getCurrentUrl().contains("samplePage.jsp"))
{
throw new IllegalStateException("This is not the XXXX Sample page. Current URL: " + driver.getCurrentUrl());
}
// for dynamic pages, wait for a specific element to signal the dynamic load is complete
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(dynamicPageLoadLocator));
}
public void clickButtonX()
{
// no wait needed here
driver.findElement(buttonXLocator).click();
}
public void clickDynamicLink()
{
// clicking this link triggers part of the page to change, reload, etc. ...
driver.findElement(dynamicLinkLocator).click();
// ... so after the click, we wait for the dynamic part of the page to finish by locating an element that is inside the dynamically
// loaded portion and wait for it to be visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(dynamicSectionElementLocator));
}
}
where this is declared in some Utils class or whatever.
/**
* Waits for the browser to signal that the DOM is loaded. For dynamic pages, an extra wait may be necessary.
*/
public static void waitForPageLoad(WebDriver driver)
{
new WebDriverWait(driver, 30).until(new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver webDriver)
{
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
});
}
Now with this framework, we no longer have to add code to each button to make sure that it's available before the click. I would suggest you take this approach to all your code. Wait only in the places it's needed rather than sprinkling waits everywhere.
Wait for the page to load
After triggering a dynamic page change, wait for the page change to complete
If you just do those two things, you will only have waits in very specific places and not need them everywhere (reducing complexity and confusion when debugging, etc.).

The screen is not navigating even the click() method is executed successfully in mobile web app using selenium web driver

I was trying to click a button on my mobile web app, using selenium web driver. The button is located, the text over the button can be derived and even the click event is performing well. But the navigation doesn't occur.
I tried with Click() method, sendKeys() method and also with script executor. But couldn't process further on.
CODE:
public class TestWeb
{
WebDriver driver;
private Selenium selenium;
#Before
public void setUp() throws Exception {
driver = new IPhoneDriver();
driver.get("http://10.5.95.25/mobilebanking");
}
#Test
public void TC() throws Exception {
System.out.println("page 1");
Thread.sleep(5000);
WebElement editbtn1 = driver.findElement(By.id("ext-comp-1018"));
String s1 = editbtn1.getText();
System.out.println(s1);
editbtn1.click();
editbtn1.sendKeys(Keys.ENTER);
((JavascriptExecutor)driver).executeScript("arguments[0].click;", editbtn1);
System.out.println("ok");
}
#After
public void tearDown() throws Exception {
System.out.println("*******Execution Over***********");
}
}
I tried click, sendKeys and ScriptExecutor separately and also combined. It is executing without any error but the navigation doesn't occur.
Does anybody can help me with some other ways to perform click function on the button?
Ram
This may not be your issue but I noticed "ext-comp-" and guess you are using extjs.
I'm using GXT and while finding by id worked for many things, on some submit buttons it didn't.
I had to use firebug in firefox to locate the element and copy the xpath.
Then I could click the element by
driver.findElement(By.xpath("//div[#id='LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click(); // worked
It was failing silently for me too. My submit button has the id of LOGIN_SUBMIT so I don't know why the following failed but ....
driver.findElement(By.id("LOGIN_SUBMIT")).click();//failed
Edit:
Here is an exact example (case 1 of 2):
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")));
//wait.until(ExpectedConditions.elementToBeClickable((By.id("gwt-debug-LOGIN_SUBMIT")))); <!-- id works as well
OK so the element is found. It will timeout and throw an exception if it is not.
Still, the following fails (under firefox, works with chrome) with no error and the page does not navigate.
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")).click();
//driver.findElement(By.id("gwt-debug-LOGIN_SUBMIT")).click(); <-- fails too
What I have to do is:
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();
So my experience was that even if I found the element with xpath, clicking failed unless I used a complete xpath.
Here is another exact example (case 2 of 2):
I can find an element like so:
WebElement we = driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]"));
I know I have found it because I can see the text via:
we.getText();
Still selecting by the path I found it fails.
//get outta town man the following fails
driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]")).click();
In this case there is not more explicit xpath to try as in case 1
What I had to do was use css:
//bingo baby works fine
driver.findElement(By.cssSelector("div#text" + i + ".myChoices")).click();
Actually, I obtained the css path via firebug than shortened it.
//this is what I recieved
html.ext-strict body.ext-gecko div#x-auto-0.x-component div#x-auto-1.x-component div#x-auto-3..myBlank div#choicePanel1.myBlank div.x-box-inner div#text3.myChoices //text3 is the id of the element I wanted to select
Whether or not you can figure out your needed xpaths and css selectors, I don't know, but I believe I experienced exactly what you did.

How do you get selenium to recognize that a page loaded?

In certain unknown situations selenium does not detect that a page has loaded when using the open method. I am using the Java API. For example (This code will not produce this error. I don't know of an externally visible page that will.):
Selenium browser = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
browser.start();
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");
When the error occurs, the call to 'open' times out, even though you can clearly see that the page has loaded successfully before the timeout occurs. Increasing the timeout does not help. The call to 'type' never occurs, no progress is made.
How do you get selenium to recognize that the page has loaded when this error occurs?
I faced this problem quite recently.
All JS-based solutions didn't quite fit ICEFaces 2.x + Selenium 2.x/Webdriver combination I have.
What I did and what worked for me is the following:
In the corner of the screen, there's connection activity indicator.
<ice:outputConnectionStatus id="connectStat"
showPopupOnDisconnect="true"/>
In my Java unit test, I wait until its 'idle' image comes back again:
private void waitForAjax() throws InterruptedException {
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if ("visibility: visible;".equals(
selenium.getAttribute("top_right_form:connectStat:connection-idle#style"))) {
break;
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
You can disable rendering of this indicator in production build, if showing it at the page is unnecessary, or use empty 1x1 gifs as its images.
Works 100% (with popups, pushed messages etc.) and relieves you from the hell of specifying waitForElement(...) for each element separately.
Hope this helps someone.
Maybe this will help you....
Consider the following method is in page called Functions.java
public static void waitForPageLoaded(WebDriver driver) {
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver,30);
try {
wait.until(expectation);
} catch(Throwable error) {
Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
}
}
And you can call this method into your function. Since it is a static method, you can directly call with the class name.
public class Test(){
WebDriver driver;
#Test
public void testing(){
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
Functions.waitForPageLoaded(driver);
}
}
When I do Selenium testing, I wait to see if a certain element is visible (waitForVisible), then I do my action. I usually try to use an element after the one I'm typing in.
Using 'openAndWait' in place of 'open' will do the trick.
From the website:
Many Actions can be called with the "AndWait" suffix, e.g. "clickAndWait". This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load.
Enabling the 'multiWindow' feature solved the issue, though I am not clear why.
SeleniumServer(int port, boolean slowResources, boolean multiWindow)
SeleniumServer server = new SeleniumServer(4444, false, true);
Any clarification would be helpful.
I've run into similar issues when using Selenium to test an application with iFrames. Basically, it seemed that once the primary page (the page containing the iframes) was loaded, Selenium was unable to determine when the iframe content had finished loading.
From looking at the source for the link you're trying to load, it looks like there's some Javascript that's creating additional page elements once the page has loaded. I can't be sure, but it's possible that this is what's causing the problem since it seems similar to the situation that I've encountered above.
Do you get the same sort of errors loading a static page? (ie, something with straight html)
If you're unable to get a better answer, try the selenium forums, they're usually quite active and the Selenium devs do respond to good questions.
http://clearspace.openqa.org/community/selenium_remote_control
Also, if you haven't already tried it, add a call to browser.WaitForPageToLoad("15000") after the call to open. I've found that doing this after every page transition makes my tests a little more solid, even though it shouldn't technically be required. (When Selenium detects that the page actually has loaded, it continues, so the actual timeout variable isn't really a concern..
Not a perfect solution, but I am using this method
$t1 = time(); // current timestamp
$this->selenium->waitForPageToLoad(30);
$t2 = time();
if ($t2 - $t1 >= 28) {
// page was not loaded
}
So, it is kind of checking if the page was not loaded during the specified time, so it is not loaded.
another idea is to modify AJAX API (to add some text after AJAX actions).
After ajax action was finished, before return, set invisible field to TRUE, selenium will find it and read as green-light
in html:
<input type='hidden' id="greenlight">
in selenium
if(driver.findElement(By.id("greenlight")).getAttr("value").equals("TRUE")){
// do something after page loading
}
If you page has no AJAX, try to seek footer of page (I also use Junit fail(""), you may use System.err.println() instead):
element.click();
int timeout =120;
// one loop = 0.5 sec, co it will be one minute
WebElement myFooter = null;
for(int i=0; i<timeout; i++){
myFooter = driver.findElement(By.id("footer"));
if(myFooter!= null){
break;
}
else{
timeout--;
}
}
if(timeout==0 && myFooter == null){
fail("ERROR! PAGE TIMEOUT");
}

Categories

Resources