Headless/GUIless automation with Selenium Webdriver - java

I would like to save some resources on my low-spec Windows boxes by running the browser in a headless mode. As far as I am aware, PhantomJS + GhostDriver is the standard choice for such task to be used with Selenium Webdriver. However after trying it and immediately running into issues with alerts handling which doesn't seem to be supported by PhantomJS. Specifically, the following exception is returned:
[ERROR - 2016-08-01T04:24:24.894Z] RouterReqHand - _handle.error - {"name":"Invalid Command Method"," . . . "}
as a result of not supporting getAlertText WebDriver Command when performing:
Alert alert = driver.switchTo().alert();
and specifically this method implemented in EventFiringWebDriver:
public Alert alert() {
return targetLocator.alert();
}
I am looking for an alternative approach or a feasible workaround. Anyone?

I have been able to work around that by executing the alert handling using JavaScript directly like this:
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("window.alert = function(){}");
jsExecutor.executeScript("window.confirm = function(){return true;}");
At the moment, there seem to be no way to perform that operation directly via WebDriver interface for PhantomJS.

Related

Click is happening some other place on Cart ICON in MSITE from appium code - Chrome browser

I am trying to click on cart icon on top right corner from Appium in chrome browser mobile.
Code to click :
driver.findElement(By.xpath("//a[#href='/viewcart']")).click();
URL : https://www.2gud.com/?cmpid=2G108229
Note: Please open this URL in mobile device and verify.
Error : Code is clicking somewhere else on mobile device.
This is working. Checked in Android 7.1 emulator
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
public class Demo {
public static WebDriver driver = null;
public static void main(String args[]) throws InterruptedException {
System.out.println("Launching the chrome driver ");
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver40.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
driver = new ChromeDriver(options);
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
driver.get("https://www.2gud.com/?cmpid=2G108229");
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
Thread.sleep(3000);
System.out.println(driver.getTitle());
driver.quit();
}
Try to click using mouse actions or JavaScript executor.
Try using the following code.
Xpath to find the view cart: //a[contains(#href,'viewcart')]
Executing a click via JavaScript has some behaviors of which you should be aware. If, for example, the code bound to the onclick event of your element invokes window.alert(), you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.
// Assume driver is a valid WebDriver instance that has been properly instantiated elsewhere.
WebElement viewCart = driver.findElement(By.xpath("//a[contains(#href,'viewcart')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", viewCart);
You should also note that you might be better off using the click() method of the WebElement interface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.

Selenium Webdriver Java: Unable to perform click operation by Actions in Chrome

I am using Selenium Webdriver(Java) for my Automation. For one of my use-case, I need to click based on co-ordinates. I am using following code to perform this operation:
Actions act = new Actions(driver);
act.moveByOffset(236, 92).click().perform();
Above code is working perfectly in Firefox(Gecko driver). But with Chrome driver, it is not working. Any idea?
Is there any other way to perform this.
I think you can try what Santosh has suggested
act.moveByOffset(236, 92).click().build().perform();
However, this should not make much of a difference as the perform() already contains the build action but this might be a workaround for your problem.
If you can locate the webelement, you can use JavaScript to perform the click this way:
JavaScriptExecutor js = (driver)JavaScriptExecutor;
js.executeScript("arguments[0].click();", element);

How to handle authentication popup with Selenium WebDriver using python?

In this question an answer is given on how to handle authentication popup with Selenium WebDriver using Java. Now, can this be used also for python? It looks like the python driver has no class UserAndPassword like the Java pendant has.
Here is what I have tried:
wait = WebDriverWait(driver, 10)
alert = wait.until(ExpectedConditions.alertIsPresent())
alert.authenticateUsing(UserAndPassword("user","passwd"))
Indeed there is no such class in python selenium library, but you can easly navigate to given alert and input user and password using other means.
wait = WebDriverWait(driver, 10)
alert = wait.until(ExpectedConditions.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys('username')
alert.send_keys(Keys.TAB)
alert.send_keys('password')
alert.accept()
This will work on standard authentication, if it is custom made one you will probably have tinker with it abit.

mouse hover is not working in safari ,selenium web driver

I used the Actions to mouse hover in FF and chrome , it is working fine. But the same code is not working in safari. I am using mac OS and selenium webdriver and java.
I tried below code.
new Actions(driver).moveToElement(element).build().perform();
JS:
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(mouseOverScript, element);
It is a show stopper issue. any help is appreciated.
Performing mouse hovers requires using WebDriver's Advanced User Interactions API. In the Java language bindings, this is accomplished by the Actions class. Unfortunately, at present, the SafariDriver does not implement the Advanced User Interactions API, so you cannot accomplish this directly. Until it is implemented, you could probably simulate it using JavaScript to fire the events fired my a mouse hover.

WebDriver : SendKeys(Integer) NOT working in Firefox 29

I am using Firefox 29 and WebDriver java 2.41.0 bindings to automate test scenarios. Have one scenario to input an integer to a input-box which was working FINE with Firefox 28 and now failing with v29 i.e latest FF version. The code I wrote for the same is:
int inputString = 123456;
driver.FindElement(By.Id("tinymce")).SendKeys(inputString);
Please help me getting through of this.
This will be the result of this issue:
https://code.google.com/p/selenium/issues/detail?id=7291
Fixed by this revision in the Selenium code:
https://code.google.com/p/selenium/source/detail?r=afde40cbbf5c
Quick test below worked for me. I understand JS is not the right way to do browser simulation, one should always FIRST use webdriver methods since they use browsers native api, but thought it would unblock you while the bug is fixed in selenium
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("http://yizeng.me/2014/01/31/test-wysiwyg-editors-using-selenium-webdriver/");
WebElement frame = driver.findElement(By.id("tinymce-editor_ifr"));
driver.switchTo().frame(frame);
WebElement body = driver.findElement(By.id("tinymce"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].innerHTML = '<h1>Heading</h1>Hello There'",body);
Can you check following points
Can you make sure there are no application changes, I mean the locator is returning single node element.
Can you also post the exception that you are getting. To troubleshoot this problem, need these details.
One thing I do before sending a value to a box is to clear it, also, the value I send is always a string, the parsing should be done by the page/code as you need to validate what the user has entered.
But I do agree with skv, we need to see the actual error being thrown.

Categories