I've created a script as the I want to use in another Selenium WebDriver script:
$function() {
$("pane1").hide(300);
});
I'm trying to figure out a way to call this script in my Selenium java code.
Calling jQuery functions from Selenium is done exactly the same way as calling any other. However, there are two issues with your code:
You have $function, where you probably mean $(function. If you tried to execute the code in your question as-is you certainly got an error because of this.
Ok, let's say you fix that problem. Now you have a $(function () {...}) call. This is not harmful but it is pointless as you are essentially saying "execute this function when the page has finished its initial load". If you use Selenium the way it is usually used, it won't return control to you until the page has finished its initial load, so there is no reason to wait for the page load.
So:
((JavascriptExecutor) driver).executeScript("$('pane1').hide(300);");
Related
I wrote an automated selenium based tests for a web application and they run perfectly with fast internet connection, but unpredictable behavior with less good connection.
Web application was build so, that if duration of response on a request< of some action at the web page, is bigger than 250ms, then appers loader-wrapper element, that prevents any kind of action from user, until response ends. Loader-wrapper can apper at any request in any place of test execution, so i cant use explicit waits of selenium, because i dont know when and where it will appear. As a result i receive an exception:
org.openqa.selenium.WebDriverException: Element is not clickable at point (411, 675). Other element would receive the click:(.show-component .loader-wrapper)
Is there any way to set a "global wait", which will stop test execution if loader-wrapper appered and will wait until it ends, and then test execution will continue? Or any another idea.
I kind of like your idea of the annotation, but not sure how to implement it.
Another possible approach is to write your own ExpectedCondition "loaderWrapperDisappeared" (or something like that), which would wait for the loader wrapper to be gone, and return the target WebElement so that you could chain a click to it.
You would then use it like this;
(new WebDriverWait(targetWebElement, 50))
.until(ExpectedConditions.loaderWrapperDisappeared(By.id("your div id"))).click();
(pardon the syntax it that's wrong...I haven't written java in a few years)
In case of web driver, you can have to use like this.
WebElement webElement = (new WebDriverWait(driver, 50))
.until(ExpectedConditions.elementToBeClickable(By.id("your div id")));
Here 50 refers to 50 seconds.
For more details, refer below the link.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html#WebDriverWait-org.openqa.selenium.WebDriver-long-
If I understand correctly you are looking for invisibilityOfElementLocated.
You can add it as a decorator to your steps...
Hope this helps!
I am trying to find a unique locator of the "update profile picture button" in my Facebook account to use it for automation test (selenium webdriver with java).
I use driver.findElement(By.className("_156p")).click();, but it doesn't work.
What should I use?!
Being unable to see the rest of the html source, I can't say for sure, but my guess is that the class is actually defined earlier in the source. The problem with using just a class name is that class names do not have to be unique on a page. The unique version of a class name is id, and if you are testing code that you can edit, try to use lots of ids.
If that's not the case, a decent way to deal with code that you can'y edit yourself is to use a css selector. Really good information on doing them here.
Another good debugging option is to use javascript or python to run a webdriver from the terminal. Because these languages are not compiled, you can run them in real time, which can allow you to tweak a class name a lot quicker. If you don't have experience with python, check this out. By using python/javascript you can create the webdriver, and then keep typing in driver.find_element_by_whatever("value_to_find") while on the same page. This will be much quicker than running the java program from scratch for every different "value_to_find".
As the page source you have provided is pretty limited unable to come up with a stable locator. How about getting the div using the text through xpath.
"//div[.='Update Profile Picture']"
Try This:
WebElement element = driver.findElement(By.className("_156n"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Worked for me.
We have a Keyword driven framework which was developed using Selenium Webdriver.
While running the scripts some test cases are getting timed out in the first run. When I do the second run the same test cases which failed last time pass but this time some other test cases fail.
Can someone please advise if anything needs to be done on the Framework/Configuration part.
I am using IE9, Java 6, Selenium 2.40 on Windows 7 and IE driver from the official Selenium website.
Your tests could be brittle because of various reasons.
1. Synchronization- DO NOT USE Thread.sleep. You should consider waiting mechanism in your tests.
There are two types of waits in WebDriver. Implicit wait and Explicit
wait.
a. Implicit wait- For example below WebDriver will internally poll at max for 30 seconds before throwing NoSuchElementFoundException
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
b. Explicit wait- Here you are telling WebDriver to wait for a certain condition to satisfy. For example, below I am waiting the link Account to be available to click. Once its available WebDriver will return me the WebElement so that I can click on it. Take a look at some already implemented useful ExpectedConditions
WebDriverWait wait = new WebDriverWait(driver,30/*Timeout in seconds*/);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account")));
element.click();
2. Data Dependency- Make sure your tests are independent of each other and they don't share data. Tests can collide if they are sharing data which makes them brittle
3. Use CSS over Xpaths - Find my answer here as to why
4. A layer of abstraction- Make sure you have abstracted test logic from page logic. Use PageObjects and PageFactory technics for better maintenance of your suite
Finally read Simon Stewarts blog on Automated Web Testing: Traps for the Unwary for details.
I would suggest making sure that the keywords you are using are not dynamically generated.
I worked on a site once where all of the ID's looked like this: 'ext-gen123', then the next run through, the very same element would have the ID: 'ext-gen124', but the run through after that would have the ID back to 'ext-gen123'...
In this case, you'd have to use some other identifier to locate the element--maybe a CssClass or an XPath.
Sainath,
The Secret to run TestCases efficiently lies in the Way You wrote the TestCases.. Use of Fluent wait, Wait before the Element is Visible and Enabled and use of html ID's in Testcases makes the TCs Efficient. IE and Selenium Does not get along almost all time.. i mean,, There are so many Issues with IE and Selenium.
The Only way to Achieve Efficiency is by Proper Handling of Exceptions and Use of Wait statements
I have an issue with selenium webdriver and i would be very grateful if anyone can help me
Environment:
selenium-server-standalone-2.31.0.jar / selenium-server-standalone-2.35.0.jar
IEDriverServer.exe (tried version 2.28 - 2.35)
Sample code:
WebElement href = this.findElement(By.xpath("//A"));
href.sendKeys(Keys.ENTER);
href.click();
Problem: A fix to any of this would help me
href.sendKeys() successfully simulates user click, but does not wait for page to load
href.click() fails to simulate user click, but successfully wait for page to load
I have search for the source code of .click() method to try to manually create a waitForPageToLoad function, but i haven't been able to find it.
I know i am not giving to much information because the application I am running the test against is internal, so I cant share a link for debugging. But any idea or previous experience with similar problems that could help me figure out what is going on would be appreciate it.
Right now, I have to do both sendKeys and click to achieve the expected results.
Whenever you do .click() on a button or link an internal method something like "waitTillPageLoads()" is called and hence webdriver waits until the next page is loaded completely.
I have experienced a scenario where it waited for almost 10-15 minutes for the page to load because web app was too slow. Wierd it sounds i know.
however sendKeys() doesn't wait. Because sendkeys is not used for clicking purpose rather it used for entering keys.
I guess there must be some strong reason for you to do
href.sendKeys(Keys.ENTER);
And if you still want to go with this approach you can implicitly you can wait for element of next page using implicit wait
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath")));
Please note :
You quotation
href.click() fails to simulate user click, but successfully wait for page to load
Can be because you have not set your browser level to 100%.
Please Check my answer at Unable to run Selenium script on IE
What you can do here is after sendKeys operation, put several waits for elements which are on the resulting page using WebDriverWait. This will serve your purpose.
Here's what I do:
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
// do something, then navigate to a different page
// (window focus is never changed in-between)
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
The link "mylink" does exist, the first invocation of click() always works. But the second click() sometimes seems to work, sometimes not.
It looks like the click() event is not triggered at all, because the page doesn't even start to load. Unfortunately this behaviour is underterministic.
Here's what I already tried:
Set longer time timeout
=> did not help
Wait for an element present after loading one page
=> doesn't work either since the page does not even start to load
For now I ended up invoking click() twice, so:
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
// do something, then navigate to a different page
// (window focus is never changed in-between)
selenium.click("link=mylink");
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
That will work, but it's not a really nice solution. I've also seen in another forum where someone suggested to write something like a 'clickAndWaitWithRetry':
try {
super.click("link=mylink");
super.waitForPageToLoad(60000);
}
catch (SeleniumException e) {
super.click("link=mylink");
super.waitForPageToLoad(60000);
}
But I think that is also not a proper solution....
Any ideas/explanations why the click() event is sometimes not triggered?
Sometimes, seemingly randomly, Selenium just doesn't like to click certain anchor tags. I am not sure what causes it, but it happens. I find in those cases w/ a troublesome link instead of doing
selenium.click(...)
do
selenium.fireEvent( locator, 'click' );
As others have stated above me, I have specifically had issues with anchor tags that appear as follows:
<a href="javascript:...." >
I've done selenium for awhile, and I really have developed a dislike for waitForPageToLoad(). You might consider always just waiting for the element in question to exist.
I find that this method seems to resolve most weird issues I run into like this. The other possibility is that you may have some javascript preventing the link from doing anything when clicked the first time. It seems unlikely but worth a double-check.
I just tried WebDriver (Selenium 2.0) and found that WebElement#sendKeys(Keys.ENTER) works.
Selenium click() event seems not to be always triggered => results in timeout?
Try selenium.pause before Selenium.click command. I have tried all above but none of them seems to resolve our problem. So finally we got a Magic selenium.pause which solved problem for me..
Hope this will solve your problem as well
I am running into this issue now also. From my usages of this, it seems like the following is the most consistent:
#browser.click(selector, {:wait_for => :page})
Not exactly sure why that would be. But it seems that if you do:
#browser.click(selector)
[maybe some stuff here too]
#browser.wait_for(:wait_for => :page)
Then you could end up waiting for a page that has already been loaded (i.e. you end up waiting forever).
I dug into the Selenium source code and found this nugget:
def click(locator, options={})
remote_control_command "click", [locator,]
wait_for options
end
...
# Waits for a new page to load.
#
# Selenium constantly keeps track of new pages loading, and sets a
# "newPageLoaded" flag when it first notices a page load. Running
# any other Selenium command after turns the flag to false. Hence,
# if you want to wait for a page to load, you must wait immediately
# after a Selenium command that caused a page-load.
#
# * 'timeout_in_seconds' is a timeout in seconds, after which this
# command will return with an error
def wait_for_page(timeout_in_seconds=nil)
remote_control_command "waitForPageToLoad",
[actual_timeout_in_milliseconds(timeout_in_seconds),]
end
alias_method :wait_for_page_to_load, :wait_for_page
Basically, this is doing the following:
#browser.click(selector)
#browser.wait_for(:wait_for => :page)
However, as the comment states, the first thing necessary is to use the :wait_for command immediately after.
And of course... switching the order puts you into the same wait forever state.
#browser.wait_for(:wait_for => :page)
#browser.click(selector)
Without knowing all the details of Selenium, it seems as though Selenium needs to register the :wait_for trigger when it is passed as an option with click. Otherwise, you could end up waiting forever if you somehow tell Selenium to wait the very instant before :wait_for is called.
Here this one will work:
selenium.waitForPageToLoad("60000");
selenium.click("link= my link");
I had the same problem - with Selenium 1.0.12 and Firefox 5.0 ; I managed to make the automated tests work this way:
I removed all "AndWait" commands (sometime they hang the test/browser)
I added a pause before the click
I added a waitForVisible after the click (usually I wait for the next html control I want to interact with on next page).
It goes like this:
waitForVisible OK
pause 1000
click OK
waitForVisible link=Go
pause 1000
click Go
etc...
It seems that the "waitForVisible" is triggered too soon, i.e. before the event handler are plugged into the control (thus clicking on the control has no effect). If you wait for 1 second, it's enought to plug/activate the click handlers...
The page has not loaded properly when you are clicking on it. Check for different elements on the page to be sure that the page has loaded.
Also, wait for the link to appear and be visible before you click on it.
Make sure you are increasing the timeout in the correct place. The lines you posted are:
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
This wait is for the page to load that comes back After the click. But the problem you describe is that it is failing when trying to do the click. So, make sure to increase the wait Before this one.
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
// do something, then navigate to a different page
// (window focus is never changed in-between)
// after the last click in these steps:
selenium.waitForPageToLoad(60000);
// anything else that happened after that
selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
If you're using FireFox, make sure you're using 3.6 or later.
WaitForPageToLoad uses the javascript variable 'readyState', but Firefox only supported this in 3.6. Earlier versions just don't wait
(see org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad)
I am having the same issue :( with selenium IDE 1.0.10 , phpunit 3.5 , selenium RC server 1.0.3
EDITED:
The culprit seems to be browser FF 3.6.13 , after upgrade to FF 3.6.14
all my errors are gone . My tests are working like charm :).
Selenium IDE 1.0.10
PHPUnit: 3.5.6
Selenium Server:selenium-2.0b1 (selenium-2.0b2 is buggy)
selenium.click("link=Continue to this website (not recommended).");
Thread.sleep(5000);
I've been having the same issue and found that I have to get the text of the link first. I know it's not the ideal way to do it, but I'm fortunate my links are uniquely named.
C# code:
var text = Selenium.GetText(myLocator);
Selenium.Click("link=" + text);
Try this:
selenium.fireEvent(ID, "keypress");