Simulating Zoom in Chrome with Selenium - java

I'm trying to do a test that involves browser zooming in Selenium (Java). The following was my approach:
WebElement html = m_webDriver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
This worked fine in IE, but causes an exception in Chrome saying "cannot focus element", which happens whenever I attempt to send any keys in Chrome.
The following sources have also had similar issues, but no workaround listed works for me:
https://sqa.stackexchange.com/questions/2023/webdriver-api-failed-to-send-keys-because-cannot-focus-element-better-work
https://code.google.com/p/selenium/issues/detail?id=2328&noredir=1
Thanks!

I'm not 100% sure, but it is probably best to use JavaScript for that.
Something along the lines of:
driver.executeScript("document.body.style.zoom=1.0"); // or 0.9 or whatever

Related

Java Phantomjs behaving different in windows and linux

I been trying to solve this issue but not sure what's the cause for this,
i made a program with java and phantomjs, and in theory they both should behave the same way.
Phantomjs in both the pc and the server is the same v.2.0.
In windows(v.7) mi testing program works as expected, but in linux (debian) the program fails, with this error when it tries to click an element:
Caused by: org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with css selector
Now, i know sometimes we have to wait for few seconds for the page to load, which already do (even added a few more just to be sure) and it was tested correctly in windows.
i tried with the click function:
element.click();
with a javascript code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element)
and with actions too:
Actions action = new Actions(driver);
action.moveToElement(element).contextClick().build().perform();
//and this code too
action.moveToElement(element).click().build().perform();
Anyone had this same or similar issue? what i else i can do? i'm stuck since yesterday and i wan't able to figure out this problem.
note: for now i have an alternate way to get page without having interaction, but still having different results from the web page when executing in different OS, for example:
where i normally i get this link: www.somesite.com/?search=xxxxx&date_in=dd/MM/yyyy&params=etc. , instead i get www.somesite.com/?search=xxxxx, the default search without any especific search parameters.
You need to use onResourceError callback, to find out what's going wrong.
You also need to use the following Command-Line options:
--ignore-ssl-errors=true --ssl-protocol=any --debug=true
See also: this issue.

selenium testcase failing with IE browser while it's working fine with chrome and firefox

While I am executing testcases (Selenium WebDriver + Java) on Internet explorer i am getting exception "unhandledAlertException" and testcases getting failed while it's working fine with Chrome and Firefox browser. Also having issue like: sometimes it click on element and respond accordingly and sometimes its clicked but did not respond with same code.
Here is some code from where it was throwing "unhandledAlertException"
protected void clickOnElement(WebElement element)
{
if(isElementPresent(element))
{
if(getBrowserName().equalsIgnoreCase("internet explorer")){
element.sendKeys(Keys.ENTER);
wait(2000);
System.out.println("In IE clicked on element");
}else{
element.click();
}
implicitWait(Integer.parseInt(ConfigParser.getProperty("common_wait_timeout")));
}
}
unhandledAlertException is now resolved by removing implicit wait from code. but sometimes unresponsiveness of elements in IE is still persist.
How can overcome from such issues with IE
Try adding proper wait conditinos in your code for IE browser. Generally IE doesnt respond well. Also have a look at the element Id's in IE they might be changing.
IE will always have issues with selenium webdriver. Microsoft itself doesn't maintain IE driver for selenium like chrome and FF. You best chances are to use proper wait.
What version of IE are you using? There are known issues with the latest versions of IE with Selemium. I faced similar issues and had to switch back to IE8. Also are you getting the exception after the browser is launched? Tip: Under IE options, make sure all the modes are enabled under the security tab and the zoom is set to 100%.
Hope this helps.. good luck!!
if (browser.equalsIgnoreCase(IE)) {
System.setProperty("webdriver.ie.driver", getRootDir() + "/src/main/java/configuration/IEDriverServer.exe");
driver = new InternetExplorerDriver();
you have to download IEDriverServer.exe and set the path in the above, and then get the driver of IE, it will work fine and i hope you will get any problem if it is even IE 11/click problem.

Chrome WebDriver doesn't select from dropdown

I have got the following code working on Firefox, but not on Chrome.
Select selectSize = new Select(dropDownListBox);
selectSize.selectByIndex(1);
I've tried using explicit wait and Thread.sleep(), but couldn't make it work.
When I debug the code, I found out that it actually grabs the value, but doesn't update the UI.
When I use System.out.println(selectSize.getFirstSelectedOption().getText()); it actually prints the size.
I've also tried running this code on iPhone and iPad emulators and it worked on there as well. I feel like this only doesn't work with Chrome Driver.
Has anyone had this issue before or do you have any suggestions?
Update: I'm using Selenium 2.41.0 and Chrome WebDriver 2.9.
Thanks in advance

Selenium rotate iOS device with Java

I have an issue that I cannot seem to resolve. I am writing an automated testing application in Java using JUnit/Selenium with Appium to test a website in the iOS simulator (Mobile Safari).
In this automated testing application I want to run some tests, rotate the device and run another set of tests. I've got everything working correctly except the rotation piece.
I found the following thread on how to rotate the iOS device in which someone suggested using the following Java code:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((Rotatable)augmentedDriver).rotate(ScreenOrientation.LANDSCAPE);
However I am getting the following cast exception:
java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be
cast to org.openqa.selenium.Rotatable
Has anyone else encountered this issue and know a fix/workaround? Maybe using Appium's JSON wire protocol?
Any help is greatly appreciated.
Thanks!
OK, so I finally figured out the issue. Annoying that after hours of searching and trying different things it was a very simple solution.
All that is needed is the following Capability:
cap.setCapability("rotatable", true);
Then use the following code to rotate the device:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((Rotatable)augmentedDriver).rotate(ScreenOrientation.LANDSCAPE);
YEAH! Problem solved!
Rotation is not implemented and this request is marked as "won't fix". Check here.
As far as i know, this is because rotating screen is already implemented in Appium so yes, check the Appium API for solution. Alternatively you can try to execute javascript, something together with target.setDeviceOrientation, but I would go with the first one.

SeleniumException: ERROR: There was an unexpected Alert!

I get this error while running selenium RC tests against IE7.
com.thoughtworks.selenium.SeleniumException: ERROR: There was an unexpected Alert! [error:[object Error]]
It happens whenever I attempt to click a link or a tab or anything clickable. Any Ideas guys. This is really frustrating.
Checking the Google search results for your error message, and the selenium development mailing list, it sounds as if this is not a bug in Selenium but rather it is Selenium telling you that an unexpected window.alert() function was called in your web application, and that Selenium does not know how to handle it because you did not tell it how to.
I'd be surprised if the same issue isn't occurring in other browsers. Perhaps your application behaves differently depending on the browser? Basically Selenium is reporting that it can't move onto further commands after you are clicking on these links or tabs because it has detected an unexpected JavaScript alert. You wont see these alerts because Selenium consumes them. There are a few things you can do to work out what's going on.
The first thing I'd suggest is to just check your application in IE7. Manually complete the steps of your tests - do you see the JavaScript alerts? If so, you will need to add the appropriate commands to your Selenium test.
If for some reason you can't replicate the alerts manually you can either dismiss the alert by using the the getAlert command, or use the response from getAlert to find out the text of the unexpected alert.
Java/TestNG example for finding out the message of the alert:
assertEquals(selenium.getAlert(), "Hello World");
The above will still cause your test to fail (unless the alert really does say 'Hello World'), but will fail with a message similar to "expected 'Hello World' but was 'Your unexpected alert message'".
I'm getting a similar thing.
No error in IE, JS error popup in Chrome, nothing in FF...until I run my Selenium script and it reports an unexpected error.
Amusingly when I do selenium.IsAlertPresent() it returns false, even though it says there was one.
I'm seeing the same error. Normally we run our test using C#, but in this case we are setting up some simple tests using the Selenium IDE.
Since Selenium IDE records your events and then allows you to play them back you would expect it to handle the alert. When looking at the events it records it appears it's capturing the alert. However, when you play it back the events are out of order.
First it captures the click event. Then is uses assertAlert to make sure the alert happened.
The problem occurs when you play it back. It does the click event and then errors out. If you move the assertAlert statement before the click event it doesn't work. If you change the assertAlert to waitForAlert that doesn't work either. I can't seem to figure out anyway to get the playback working in the Selenium IDE. If I didn't need this runnable for a non-techy I'd just put it into my regular tests and be done with it.
Edit: After further review it appears the alert message isn't matching. Even though it grabbed the alert itself when doing the record. Visual inspection doesn't do anything to help me determine what it considers different.
I've seen this error also. In my case the issue was the latency in an Ajax response object was causing Selenium IDE error. The solution that worked for me was to add a pause command right after any Ajax calls in the test script.
The call below was taking few seconds to return with valid Ajax response
click
css=#form3 > div.addButton > button.btn.btn-primary
By adding a pause clause, the "Unexpected alert" issue was resolved
pause
5000
I Suggest you to test manually those steps which are there in your selenium test.If it is not reproducible then it might not be the problem in the page.
please debug your selenium script with assert function for identifying where is it finding the unexpected alert.
if (selenium.isAlertPresent()) {
assertEquals(selenium.getAlert(), "Hello World");
}
which will help you identify the problem. I suggest you to repeat the test because you may find that assertion occurring in different part of your code.

Categories

Resources