Solution:
Today (2011-04-13) handles Selenium RC the confirmation boxes under Firefox 4 badly.
I had to change back to Firefox 3.16 and then this problem disappears. Thanks again.
Original Question:
Hello Selenium gurus,
I am trying to execute an automated browser test from Java using Selenium 2.0b3 as the Java client and standalone server too.
The server starts a Firefox 4 with a specific profile.
The test is stuck at a "click" command; it waits and no timeout/exception/any error happens. During this click comes up a confirmation box, so I guess that is the problem, but I do not know why this gets stuck. In Selenium IDE comes not this confirmation box, only in RC.
The problem is not that, that the click does not happens (because the confirmation box appears), but that this confirmation box hangs.
I tried these but did not help:
selenium.click("css=div[id=command_Delete]");
selenium.click("id=command_Delete");
String JSscript = "jQuery('#command_Delete').click();" // See http://api.jquery.com/click/
selenium.runScript(JSscript);
These are just ways how to start the click.
Maybe I have to start a different Thread according these link: http://www.sqaforums.com/showflat.php?Cat=0&Number=567974&an=&page=0&vc=1
Thanks: Andras
Java code:
selenium.click("//div[#id='command_Delete']/span");
//stucks here
//so this is not reached:
String confirmation = selenium.getConfirmation();
HTML:
<div id="command_Delete" class="...">
<div>...</div>
<span>Delete</span>
<div>...</div>
<br><br>
</div>
Javascript:
<script type="text/javascript">
$('command_Delete').addEvent('click',function(){
var isConfirmTrue = confirm('Do you want to delete?');
if (isConfirmTrue) {
var myForm = getFormObj(document, "deleteForm");
submitForm(myForm);
}
});
</script>
And in the Java, it hangs waiting the selenium rc to answer:
HttpURLConnection.getInputStream() line: 912 [local variables unavailable]
HttpURLConnection(HttpURLConnection).getResponseCode() line: 367 [local variables unavailable]
HttpCommandProcessor.getResponseCode(HttpURLConnection) line: 147
HttpCommandProcessor.getCommandResponseAsString(String) line: 167
HttpCommandProcessor.executeCommandOnServlet(String) line: 107
HttpCommandProcessor.doCommand(String, String[]) line: 89
DefaultSelenium.click(String) line: 167
...
Have you tried replacing
selenium.click("//div[#id='command_Delete']/span");
with
selenium.click("id=command_Delete");
?
It seems to me that since the event is binded to the div itself that should work.
Additionally, you can execute the click action directly by doing this, I'm using jQuery since you seem to already have it loaded on the page and it makes things easier (especially for cross-browser testing):
String JSscript = "jQuery('#command_Delete').click();" // See http://api.jquery.com/click/
selenium.runScript(JSscript);
You'd just need to evaluate if this solution is good for you.
If a confirmation box is popping up the test will block until that confirmation box has been removed. You can do a quick manual test to see if this is your problem.
Run your test and when the confirmation box comes up manuall y interact with it, if everything else is fine the test should continue from that point as per normal.
Selenium is waiting for the page to load, but the pop up box is blocking the page from loading which is eventually resulting in a timeout.
You can try and work around this using selenium.chooseOkOnNextConfirmation(); before your click.
Are you sure your widget can afford a click?
I would try the CSS identifier:
selenium.click("css=div[id=command_Delete]");
Related
I have an app made with Quasar. I make e2e tests with python selenium using firefox. I log in and then would like to logout. To perform this I need as usually click on profile icon to show the menu and select Logout.
The issue is that whatever I tried the menu doesn't appear. The code looks as follows.
#pytest.mark.nondestructive
def test_must_login_and_logout_properly(logged_in_selenium, wait):
driver = logged_in_selenium
avatar = wait.until(EC.presence_of_element_located((By.XPATH, '//header//img[#alt="Profile image"]/ancestor::button/parent::span')))
# "natively" via selenium
ActionChains(driver).pause(1).move_to_element(avatar).click().perform()
# via javascript
driver.execute_script('document.evaluate(\'//header//img[#alt="Profile image"]/ancestor::button/parent::span\', document).iterateNext().dispatchEvent(new Event("click"))')
The above code doesn't work UNTIL I really click this element with the mouse. By putting time.sleep(10) at first line in test function and click. After that any method via avatar.click() or execute_script(...) works fine.
I also have an option to use PyAutoGUI to find the avatar on the screen and perform that click, but I don't like this variant.
-- UPDATE
I found out why it behaves like this, but still don't know how to work this around. Initially (after page load) click event handler is not attached to //header//img[#alt="Profile image"]/ancestor::button/parent::span element. It only appears after manual click in this area.
Any ideas?
You didn't share a link to this page, so I can only guess.
I would try to:
Wait for element visibility, not just existance.
Try to remove click from ActionChains action.
Add a short delay between moving to the element and click on it..
Still not sure, but possibly this will work
avatar = wait.until(EC.visibility_of_element_located((By.XPATH, '//header//img[#alt="Profile image"]/ancestor::button/parent::span')))
ActionChains(driver).pause(1).move_to_element(avatar).perform()
time.sleep(0.2)
avatar.click()
UPD
Try this:
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='q-avatar']"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Logout']"))).click()
Eventually it was necessary to click the element twice. Due to maybe Quasar implementation detail.
#pytest.mark.nondestructive
def test_must_login_and_logout_properly(logged_in_selenium, wait):
driver = logged_in_selenium
avatar = wait.until(EC.visibility_of_element_located((By.XPATH, '//header//img[#alt="Profile image"]/ancestor::button/parent::span')))
ActionChains(driver).pause(1).move_to_element(avatar).click().pause(0.5).click().perform()
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Logout']"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[text()="You have logged out"]')))
Test cases fail while not finding a link or any validation messages after a button click.
I used explicit waits for page to load:
var waitForDocumentReady = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
waitForDocumentReady.Until((wdriver) => (WebDriver as IJavaScriptExecutor).ExecuteScript("return document.readyState").Equals("complete"));
For wait specific div of validation messages:
WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.ClassName("validationErrors")));
But test cases pass sometimes with pipeline and fails sometimes.
I would recommend following:
When ever your tests fail take a snapshot at that moment.
sometimes when you run your tests locally the network communication
is a bit faster than running on server. Which means a spinner or a
loading control may not appear locally because results were retrieved
fast enough but the loader will appear when there is delay from
server in sending the response to client.
If you are trying to run your tests in FF headless on remote grid
setup then it is not yet stable in my experience.
Identify the pattern in failed scenarios which frequently fail and
try and run them multiple times in a loop and identify the pattern of
failure. In one of my case there was an actual bug in the performance
of the application which used to get reproduced whenever the tests
ran immediately in bulk
I was having this issue, except my test would always fail in Azure Pipelines but pass locally. Not running in "headless" mode fixed my problem. To do this, make sure "Test mix contains UI tests" is checked in your "Visual Studio Test" task.
There are a couple of things you need to take care as follows:
Test cases fail while not finding a link or any validation messages after a button click: A lot depends on the attributes of the button. In case the button contains either of the following attributes/events the DOM Tree would be getting updated and subsequently trying to find the desired element may fail:
onclick() event:
<button onclick="myFunction()">Click me</button>
jQuery click() Event:
$("p").click(function(){
alert("The paragraph was clicked.");
});
So after a button click, to find the desired element you need to induce you have to induce WebDriverWait for the ElementExists().
You can find a relevant discussion in ExpectedConditions.ElementIsVisible returns TimeoutException even when element is present
ExecuteScript("return document.readyState").Equals("complete"): This line of code is not that effective to ensure if the desired element(s) are present/visible/interactable with in the HTML DOM.
You can find a relevant discussion in Do we have any generic function to check if page has completely loaded in Selenium
However to wait for specific div with ElementExists() should have worked. The relevant HTML would have helped us to analyze what went wrong. However, if the validationErrors related element is based on HTML5 Constraint validation then you have to use the GetAttribute() method.
You can find a relevant discussion in How to handle html5 constraint validation pop-up using Selenium?
I'm writing my first ever selenium test. It's amazing and I've got it logging into my application which makes me feel giddy. Here's my automated UI stuff using ChromeDriver:
var options = new ChromeOptions();
options.AddArgument("--start-maximized");
IWebDriver driver = new ChromeDriver(options);
Authentication.LogInAsAdmin(driver);
System.Threading.Thread.Sleep(1000);
TenantManagement.CreateTenant(driver, false);
Authentication.LogInAsAdmin works just fine. My single page angular app (upon login) loads additional content and displays it. TenantManagement.CreateTenant(driver, false) is where things go wrong. The first line in that piece is failing:
public static bool CreateTenant(IWebDriver driver, bool testLogin)
{
driver.FindElement(By.Id("TenantsTab")).Click();
...
}
As you can see, I have a thread sleep before this call just to make sure everything is loaded before trying to find the element. When looking at the html in Chrome dev tools (element inspector), I see:
<li ng-class="{active: active, disabled: disabled}" id="TenantsTab" ng-repeat="Tab in SelectedArea.Pages | filter:lessThanTwo" ng-click="trackTab(Tab)" active="Tab.active" heading="Tenants" class="ng-isolate-scope">
<a ng-click="select()" tab-heading-transclude="" class="ng-binding">Tenants</a>
</li>
This element is definately present when the thread wakes back up and tries to find it, but it's not being found. Any clues on what I'm doing wrong here or how one would go about troubleshooting this when using selenium?
The right way to do this seems to be using selenium wait functionality (which I just discovered). By adding this:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
to the beginning of my CreateTenant() method, selenium waits up to 10 seconds for an element to become available, and adding that worked. Not sure why the thread sleep doesn't.
Please don't make comments regarding why i posted a similar question. I have tried many things and nothing is working. Below is the HTML
<div id="businessSettingsColumn1">
<div class="sectionLink">
Business details
</div>
<div class="sectionLink">
Operating hours
</div>
<div class="sectionLink">
Closed dates
</div>
<div class="sectionLink">
Appointment notifications
</div>
I need to click the second link
I have tried
1) webdriver.findElement(By.partialLinkText("Operating hours")).click();
2)webDriver.get(mylement.findElement(By.tagName("a")).getAttribute("href"));
3)
List<WebElement> businessLinks= busCol.findElements(By.className("sectionLink"));
for(WebElement bLink :businessLinks) {
if(bLink.getText().contains("Operating hours")) {
bLink.findElement(By.tagName("a")).click();
}
}
4) Using the Action builder to move the mouse and then doing a click
Also when i did this 3 times in a row , my element got clicked
webdriver.findElement(By.partialLinkText("Operating hours")).click();
webdriver.findElement(By.partialLinkText("Operating hours")).click();
webdriver.findElement(By.partialLinkText("Operating hours")).click();
I am using Firefox version 25.0 and Selenium version 2.35.0. Funny thing though is when i do a sysout , the values get printed and when i try to get the url using webdriver I get "Element not found in the cache - perhaps the page has changed since it was looked up" .. its pretty much a static page with links only so i dont understand why i am not able to click it.. Any help will be much appreciated.
Can you try calling focus() on the element before you click on it?
This usually occurs because the element was there at some point, but then something happened and then it's no longer there for some reason. I encounter this frequently when a page makes AJAX calls for example.
Have you tried adding some waits so that selenium is sure that the element is ready?
The webpage that I'm testing is using knockout. On other pages on our site that are not currently using knockout I'm not having the same problem. The scenario I have is where the page opens, I enter in various required fields and click the save button. At some point between when it enters a value in the last text field and when it clicks the save button the fields that previously had values become cleared out, and thus the script can't continue. Here is an example of the code that I'm running:
driver.findElement(By.id("sku")).clear();
driver.findElement(By.id("sku")).sendKeys(itemNo);
driver.findElement(By.id("desktopThankYouPage")).clear();
driver.findElement(By.id("desktopThankYouPage")).sendKeys(downloadUrl);
driver.findElement(By.id("mobileThankYouPage")).clear();
driver.findElement(By.id("mobileThankYouPage")).sendKeys(mobileDownloadUrl);
driver.findElement(By.id("initialPrice")).clear();
driver.findElement(By.id("initialPrice")).sendKeys(initialPrice);
driver.findElement(By.id("submitSiteChanges")).click();
Like I said, between the time it enters text in the last field and the time it clicks save the fields that previously had text in them get cleared out, and thus my test fails. The problem is it doesn't always happen. Sometimes the test runs fine, other times it doesn't.
I've tried putting Thread.sleep(x); all over the place to see if pausing at certain points would fix the problem. I also have tried using javascript to wait in the background for any ajax that might be running. Also have the implicit wait of driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS). None of it seemingly has made any difference.
I'm running version 2.13 of selenium server and all my tests run on Firefox 8.
Any help on this would be greatly appreciated!
Firefox has a bug which prevents some events from being executed while the browser window is out of focus. This could be an issue when you're running your automation tests - which might be typing even if the window is out of focus.
The point is that knockout model updates are triggered (by default) with the change event. If it's not being executed, it's underlying model won't be up-to-date.
To fix this issue I triggered the change event "manually", injecting javascript into my tests.:
//suppose "element" is an input field
element.sendKeys("value");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("$(arguments[0]).change();", element);
As you might have noticed, I'm using jQuery to trigger the change event. If you're not using jQuery on your app, you can check here how to trigger it using vanilla javascript.
Hope that helps somebody.
I had the exact same problem. I would guess also that your code works fine in Chrome but not firefox, and that it always works when you do it manually?
Anyway, the problem is likely to be that Selenium doesn't really behave the same way as a real user, and doesnt trigger the same events. When you "submit" the form, it sometimes won't have executed the "change" event on a text area, meaning that it won't have changed.
I had the same problem testing Backbone.modelbinding, which uses the "change" event to update the model from the form. Knockout also uses the "change" event, but fortunately it can also use the "keyup" event. See valueUpdate in the docs:
<input data-bind="value: someValue, valueUpdate: 'keyup'" />
Anyway, that reproducibly solved it for me, and didnt need any sleeps once I had that done. The downside is that you'd be running the event more than is necessary in production, in order to make tests work. Another downside is that you if you want to run some code when a value changes, you'll now get one event per keypress instead of one per field change, which sucks sometimes.
There is another solution, which is to make Selenium fire the change event yourself, for example: Selenium IE change event not fired. It's also suboptimal, but what can you do.
You could also try putting the focus on a button before you click it. Don't know if that will work, I haven't tried it.
I was facing the same issue, while using JavaScriptExecutor for sending keys to text fields.
Using below code in IE (same code is working with chrome):
(JavascriptExecutor) driver.executeScript("arguments[0].value = '" + value + "';", element);
After updating the code to simple "sendKeys()" method, it resolved my issue:
element.sendKeys("some text");