PhantomJS slower than ChromeDriver, using Selenium - java

I'm trying to use PhantomJS 2.0/GhostDriver instead the ChromeDriver, since I have read I could speed up my UI tests.
This is the test code I'm running, as part of a Junit test:
#Override
public void runTestCase() throws Exception {
long startTime = System.currentTimeMillis();
// log in as admin
Login.loginAs("admin", "password");
System.out.println(System.currentTimeMillis() - startTime);
}
The loginAs function fills in the text fields for the username and password, then click on the submit button and finally moves in the home section of the new returned page.
Now, I'm running once a time this simple test using both Phantomjs and ChromeDriver as driver for Selenium in Java (v2.45).
They are initialized as follow:
ChromeDriver
System.setProperty("webdriver.chrome.logfile", workingDirectory + "\\chromedriver.log");
service = new ChromeDriverService.Builder().usingDriverExecutable(new File(workingDirectory + "\\chromedriver.exe")).build();
capabilities = DesiredCapabilities.chrome();
options = new ChromeOptions();
options.addArguments("--allow-file-access-from-files");
options.addArguments("--verbose");
capabilities.setVersion("");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(service, capabilities);
PhantomJS
System.setProperty("phantomjs.binary.path", workingDirectory + "\\phantomjs.exe");
cliArgsCap = new ArrayList<String>();
capabilities = DesiredCapabilities.phantomjs();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
cliArgsCap.add("--webdriver-loglevel=INFO");
cliArgsCap.add("--load-images=false");
capabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
driver = new PhantomJSDriver(capabilities);
I'm running my test on a 64bit Windows 7 machine. So, having a look the time took by the test, I always note that ChromeDriver is faster than PhantomJS. Always. For instance, if the test with ChromeDriver takes about 3-4 seconds, the same with PhantomJS takes about 5-6 seconds.
Has anyone experienced with this issue? Or could anyone give me any reason for that? Am I setting something wrong?
Furthermore, if you need more details, let me know.

I have found that this setting uses a lot of memory that seems to keep growing:
cliArgsCap.add("--load-images=false");
But when I use this setting the memory usage is stable:
cliArgsCap.add("--load-images=true");

"PhantomJS is a headless WebKit scriptable with a JavaScript API" as it's explained on the main page of the project.
Google split from WebKit to create Blink to use it in Chrome.
What are the main differences between them - unfortunately I'm not the expert here.
I run one of my really long scenarios both on Chrome and PhantomJS and to my surprise the difference was pretty significant:
PhantomJS - 583.251 s
Chrome - 448.384 s
Using PhantomJS doesn't bring performance benefits in my case but running tests headless does. I can use machine without graphical desktop and save computing power for some additional threads.

The slowest aspect of a web page is the downloading of html, JavaScript, css, images, etc and making AJAX request.
To anybody who says Headless is faster, how can headless address any of these?

Related

Set capability on already running selenium webdriver

In selenium test step (like a button click) i want to prevent the selenium waiting for page finish loading. I cant throw the load Exception because then i cant work with the page anymore.
Its possible to do a simmilar thing like this:
DesiredCapabilities dr = DesiredCapabilities.chrome();
dr.setCapability("pageLoadStrategy", "none");
WebDriver driver = new RemoteWebDriver(new URL("...."), dr);
What I want is like "dr.setCapability("pageLoadStrategy", "none");" but just for one specifique step.
Does anyone know a way to do this?
Capabilities are no longer editable once the browser is launched.
One way to temporary disable the waiting is to implement your own get with a script injection.
Something like this:
//
// loads the page and stops the loading without exception after 2 sec if
// the page is still loading.
//
load(driver, "https://httpbin.org/delay/10", 2000);
public static void load(WebDriver driver, String url, int timeout) {
((JavascriptExecutor)driver).executeScript(
"var url = arguments[0], timeout = arguments[1];"
"window.setTimeout(function(){window.location.href = url}, 1);" +
"var timer = window.setTimeout(window.stop, timeout);" +
"window.onload = function(){window.clearTimeout(timer)}; "
, url, timeout);
}
As of the current implementation of Selenium once we configure the WebDriver instance with our intended configuration through DesiredCapabilities class and initialize the WebDriver session to open a Browser, we cannot change the capabilities runtime.
It is worth to mention, somehow if you are able to retrieve the runtime capabilities still you won't be able to change them back.
So, in-order to make a change in the pageLoadStrategy you have to initiate a new WebDriver session.
Here is #JimEvans clear and concise answer (as of Oct 24 '13 at 13:02) related to proxy settings capability:
When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.

Selenium Grid - Unable to find elements on the 2nd page in IE11

I have some selenium tests that work great in Chrome (locally and remotely) but when I run them remotely from my machine to a Windows 2012 VM they can't find any elements AFTER clicking a button to move to the next web page.
Here is a code snippet:
DesiredCapabilities caps = null;
caps = DesiredCapabilities.internetExplorer();
caps.setBrowserName("internet explorer");
caps.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
caps.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
caps.setJavascriptEnabled(true);
caps.setPlatform(Platform.WIN8);
String nodeURL = "http://0.0.0.0:5555/wd/hub"
driver = new RemoteWebDriver(new URL(nodeURL), caps);
// IE launches correctly, goes the cart page. Calculations are done on the pricing, and we click to move to the login page
driver.findElement(By.id("userName")).sendKeys("test#test.com");
// ^^ NoSuchElementException: Unable to find element with id == userName
So obviously there is a good connection made. Actions are performed and my tests move from the cart page to the login page. I even added a wait.until, hoping that it was a timing issue, but it timed out after 15 seconds. I'm watching the driver on RDP, it moved to the page. During the 15 seconds, I even opened the dev tools and checked to see if the element was there and it was.
What's going on? Thanks!
Preface: As a QA automation engineer, I hate IE.
It turns out that http://company.com was added a trusted site, but not https://company.com. Once I added that, it started working.
Did I mention I hate IE?

Selenium Webdriver Continuing After Site from get() Prompts for Cert

I'm writing a Java app in which I'm doing a simple driver.get(url) in which the url will prompt for a cert selection. I want to try and automate the cert selection process in Firefox 33 using the AutoIt jar, but I can't even get my Java program to continue after it executes this get, since the site is in an indefinite state of loading until a cert is selected, so the execution indefinitely stays on the get itself. Is there any way around this?
I found a similar question: Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?
Ths solution given in the above link is to change the firefox settings in case the webpage is taking too long to load.
FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);
driver = new FirefoxDriver(firefox, customProfile);

Selenium Webdriver : Suppress IE log info prints on the console

Whenever I launch IE using Selenium WebDriver, it displays a log message on the console which is something like below :
Started InternetExplorerDriver server (32-bit)
2.39.0
Listening on port 5555
Is there any way to suppress this log message from not being displayed on the screen?
Thanks,
Sitam
The following snippet provides the logic but untested Java code. You might need to debug a bit.
The documentation is here, which you should have a look.
InternetExplorerDriverService.Builder ieDriverService = new InternetExplorerDriverService.Builder().withSilent(true);
WebDriver driver = new InternetExplorerDriver(ieDriverService.build());
After doing R&D for while I got solution for four major browsers.
Chrome = System.setProperty("webdriver.chrome.silentOutput", "true");
Firefox = System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "null");
IE =
InternetExplorerDriverService.Builder ieDriverService = new InternetExplorerDriverService.Builder().withSilent(true);
System.setProperty("webdriver.ie.driver", "src/main/resources/IEDriverServer.exe");
driver = new InternetExplorerDriver(ieDriverService.build());`
Edge = System.setProperty(EdgeDriverService.EDGE_DRIVER_LOG_PROPERTY,"null");
If anyone know other browser properties to suppress console output feel free to share in comments.

How to clear browser cache in Selenium test

I am running my Selenium tests with WebDriver. I am repeating the tests with some loop so now I want to Clear the cache before starting new test in JAVA.
#Test
public void ffAndIe() throws InterruptedException {
int i = 0;
while(i < 5000){
driver.get("http://dit-map.appspot.com/");
Thread.sleep(15000);
driver.get("http://dit- map.appspot.com/#home:zoom=7&lat=37.04&lng=25.05&display=weather");
Thread.sleep(15000);
driver.get("http://dit-map.appspot.com/#home:zoom=9&lat=37.55&lng=23.83&display=weather,wind");
Thread.sleep(10000);
driver.get("http://dit-map.appspot.com/#home:zoom=9&lat=37.84&lng=23.22&display=weather,wind,cloud");
Thread.sleep(10000);
driver.get("http://dit-map.appspot.com/?lang=en#home:zoom=10&lat=38.13&lng=22.59&display=weather,wind,meteogram");
Thread.sleep(10000);
i++;
}
}
with in this while loop the first thing I want to do is to CLEAR my CACHE (IE, MOZILLA & CHROME)
any idea how can I achieve this?
Thanks
Currently, there is no way to clear the cache through the web driver API. However, if you can start a new instance of the browser each time, the cache should be cleared in FF and Chrome because a new profile is created on each launch.
The comments for issue #40 (Clear cache) in the Selenium issue tracker list two potential solutions to your problem if creating a new browser instance isn't possible:
Clear the IE cache from the command line
Disable the FF cache using a custom profile
HTH
DT_IE_AGENT_ACTIVE=true activates the Add-On so that it collects data from the current browser session
Hope it help
I have used this lines of code in python to do that and it seems to clear the cache every time(Internet Explorer)
capab = DesiredCapabilities.INTERNETEXPLORER
capab.clear()

Categories