Selenium exception when attempting to click ID of a button - java

I'm attempting to use the Java Selenium client with JBehave to interact with a simple web page as a demonstration of BDD techniques.
When running the test, i'm trying to do a simple
selenium.click("joinButton");
but i'm getting a
(com.thoughtworks.selenium.SeleniumException: joinButton is not defined)
exception thrown.
I've tried qualifying the ID as a dom ID using id=joinButton, as a css selector and even as xpath but to no avail.
The element is definitely rendered in the page. I'm clearly doing something daft here.
Any pointers?
Thanks

Did you call waitForPageToLoad() after navigating to the page in Selenium? I'm betting your DOM hasn't loaded yet when your click is called.

Please confirm that while verifying with xpath and css you used selenium.click("//[#id=joinButton]") and selenium.click("css=#joinButton") respectively.
You can also keep selenium.isElementPresent("joinButton") in a loop until some timeout (30/60 sec) and the moment selenium finds this element, it would come out of this loop and execute click command.

I realised what the issue was here. The Selenium class was being extended, and the click method overridden to only accept CSS selectors. With this cruft now removed, the operation works as expected.
Thanks for your help folks.

Related

Seetest Automation MobileWebDriver find element by xpath

I'm using Seetest Automation from experitest.com, more accurately I'm using the MobileWebDriver implementation of Seetest Automation, as experitest says "...for mobile browsers [...] it can be used also to automate Native Applications as well".
So, I launch my instrumented app under Seetest, then open the inspector 'object spy'. This inspector is pretty cool because you can click in some screen element of the app and the inspector will automatically show the node in the DOM with all its properties. The inspector can validate xpath expressions of your screen DOM, so you can easily verify if you xpath expression is correct for the element your are looking for.
I have got a xpath: //*[#text='Login']
That xpath has been validated with the object spy, and it is able to locate the element I'm looking for.
So, I have a validated the xpath expression for my button, below is the Seetest client that will perform the click:
MobileWebDriver driver = new MobileWebDriver("localhost", 8889);
driver.client.setDevice(mydevice.ID);
// Launch instrumented app
driver.client.launch(myapp.activity, true, true);
driver.findElementByXPath("//*[#text='Login']").click();
The four previous lines aren't working right. ¿Something wrong? ¿Missing something? ¿Instrumented vs. not instrumented?
Exception of previous four lines:
com.experitest.client.InternalException: Exception caught while executing click: Element was not identified: 'xpath=//*[#text='Login']' at zone NATIVE
I'm using Seetest's MobileWebDriver implementation because I need compatibility/integration with some selenium/appium code.
Thanks and hope someone knows something!
The problem was the target 'zone' or 'context' to find the element. MobileWebDriver uses NATIVE zone as default. My application is an hybrid one that uses web components too, and the element that I was looking for is a web element.
So, the solution was use this method of MobileWebDriver:
((MobileWebDriver) driver).useWebIdentification();
That allows the driver to look into the WEB zone instead NATIVE.
There exists another method to check elements in NATIVE as well:
((MobileWebDriver) driver).useNativeIdentification();
Simple solution. Hope it helps.

When test is runing to watch in parallel if specific element exist

I need some help, can I somehow on every webdriver action (click() or loading page) see if specific element is visible. I am working on a project, if on a site appears any error, this element <a style="color:#FFF;" href="#debug-plugin-error"> is visible and test must fail.
I dont want on every event/action to add some method that will validate this situation, maybe junit has some rule?! Any ideas?
Check out EventFiringWebDriver which may help,
http://onlineseleniumtraining.com/how-to-working-with-listeners-in-selenium-webdriver/

Selenium Webdriver with Java: Element not found in the cache - perhaps the page has changed since it was looked up

I am initializing a variable in the beginning of my class:
public WebElement logout;
Later on in the code, in some method, the first time I encounter the logout button, I assign a value to that variable (in the brackets of an if/else statement):
logout = driver.findElement(By.linkText("Logout"));
logout.click();
I then use "logout" once more, successfully, at another stage of my test:
logout.click();
And at the end of the test, at a place where the element is the same (By.linkText ("Logout")), I get this error:
Element not found in the cache - perhaps the page has changed since it was looked up
Why?
EDIT: Actually, I dont successfully use the logout.click(); commant at another stage of my test. Looks like I cant use it again. I have to create a logout1 webelement and use it...
If there has been any changes to the page after you have initially found the element the webdriver reference will now contain a stale reference. As the page has changed, the element will no longer be where webdriver expects it to be.
To solve your issue, try finding the element each time you need to use it - writing a small method that you can call as and when is a good idea.
import org.openqa.selenium.support.ui.WebDriverWait
public void clickAnElementByLinkText(String linkText) {
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));
driver.findElement(By.linkText(linkText)).click();
}
Then within your code you'd only need to:
clickAnElementByLinkText("Logout");
So each time it will find the element and click on it, as such even if the page changes as it is 'refreshing' the reference to that element it all successfully click it.
The browser rebuilds the DOM structure of the dynamic pages, so the elements do not need to keep you have to find them before to use.
Using XPath, for example. This approach is not correct(can cause exception org.openqa.selenium.StaleElementReferenceException in the future):
WebElement element = driver.findElement(By.xpath("//ul[#class=\"pagination\"]/li[3]/a"));
...// Some Ajax interaction here
element.click(); //<-- Element might not be exists
This approach is correct:
driver.findElement(By.xpath("//ul[#class=\"pagination\"]/li[3]/a")).click();
This is because you are not giving proper time to load the page.So you have to give Thread.sleep(); code for the Given page.
I am also getting the same issue for my project but after using the Thread.sleep(); its working fine for me give the web page as much as it is possible for 30 to 50 secs.

Wait for ext-js dialog to load with Selenium

When testing an ext-js application with Selenium (WebDriver), I have a button that brings up a dialog. The test then fills out some form fields, clicks buttons, etc. The problem is that selenium starts performing these actions before the dialog is fully done building.
I know that I can tell selenium to wait for a specific element to appear, but if I want to generalize the case to "wait for the dialog to finish loading", is there a way to do that?
The short answer is, "No, theres no simple way to do that." to understand why this is so, consider this: How do you define "the whole DOM is loaded?" Is it when the last closing </html> tag is downloaded? When it's parsed and added to the DOM? Is it when the document readyState property is "interactive," "contentReady," or some other value? What about DOM elements added via JavaScript? What if that JavaScript is fired via setTimeout()? What about processing XmlHttpRequests? There is no one-size-fits-all answer to that question that is correct 100% of the time for all use cases.
Waiting for an element is the right approach. Identifying the right element can be challenging when using extJs, because of the generated IDs. I've often found it more effective to use class attributes with that particular JavaScript framework.

Can't right-click, Java Selenium WebDriver java.lang.UnsupportedOperationException: contextMenu

This is driving me crazy, I just need WebDriver to right-click on an element and the rest of my selenium will work.
I'm using Eclipse, all my .jar imports and libraries are set up correctly.
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://............/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
Much later in my code, I left-click inside the javascript portlet row I want to execute a right-click on without any problems.
selenium.click("//tr[#id[contains(.,'Equipment')]]");
That works just fine, the row that gets clicked is highlighted, as it is supposed to do.
Now I just need to right-click on it, but I can't! Trying:
selenium.contextMenu("//tr[#id[contains(.,'Equipment')]]");
fails to right-click, and returns me the error: java.lang.UnsupportedOperationException: contextMenu.
There is an element that is not considered 'visible' until that row is right-clicked. It is being detected as existing, but selenium will not run it unless I manually right-click it myself mid-run in WebDriver to make it visible. Otherwise, it just gives me this:
com.thoughtworks.selenium.SeleniumException:Element is not currently visible and so may not be interacted with.
Right-clicking on that row is what makes the element 'visible', which is why I need to have selenium right-click so badly. I have tested to make sure the not visible element is being detected as existing, it is.
boolean exists = selenium.isElementPresent("//a[#id[contains(.,'faction')]]");
boolean visible = selenium.isVisible("//a[#id[contains(.,'faction')]]");
System.out.println("Exists: " + exists);
System.out.print("Visible: " + visible);
Gives me
Exists: true
Visible: false
In other words, it's not a problem with my xpath. It's there. It's just not 'visible'. I've tried sending Shift+F10 as a replacement for right-clicking as well, no good.
This all works fine in Selenium IDE, but I need it working with WebDriver so I can mess with it in Java. Please help me out.
EDIT: Thanks to Slanec's advice and more messing around with the actions builder, I found that it's a problem with my selenium webdriver not being up to date for Firefox 14. It's odd, considering I thought I had downloaded the most recent one just last Thursday. Either way, apparently native event support for Firefox 14 was not added until webdriver 2.25.0, and I am using 2.24.1.
Unfortunately, when looking at the source code, the contaxtMenu() method has not been added to WebDriver emulation. It's not even present at the org.openqa.selenium.internal.seleniumemulation package where all the other methods (disguised classes via the Command-pattern) sit. It was, most likely, not backported to WebDriverBackedSelenium when it was introduced (Selenium RC is deprecated and not in active development, remember?).
Things you could try:
Get rid of Selenium RC, if you still can. The WebDriver API is much more clean, powerful, and actively developed :). To do it with WebDriver, you'd do
WebElement elem = driver.findElement(By.xpath("//tr[#id[contains(.,'Equipment')]]"));
new Actions(driver).contextClick(elem).perform();
Try to do just this task with the WebDriver object you created and then fall back to using Selenium again. The two lines of code above should possibly work when just thrown in between the Selenium code.
Implement it yourself. It's not that hard. Just look at the org.openqa.selenium.internal.seleniumemulation package and the WebDriverCommandProcessor class, add contextMenu() method as described above. This will mean to make your own Selenium builds from the edited source code, but hey, why not.
Last resort - figure out what action exactly the hidden element is waiting for and simulate the event via fireEvent() method. Doing the following should do the trick (if not, it's just waiting for a different event):
fireEvent("//tr[#id[contains(.,'Equipment')]]", "contextmenu")
As a side note, instead of the long
"//tr[#id[contains(.,'Equipment')]]"
or a little bit better
"//tr[contains(#id,'Equipment')]"
you could just write this:
"css=tr[id*='Equipment']"
Isn't it a little bit nicer and more readable? I'm really comfortable with XPath, too, but learning some basic CSS Selectors (which can do most, but definitely not all XPaths can do) isn't that scary...
May be you can try selenium.contextMenuAt

Categories