Specifying custom screen resolution in Selenium tests - java

As mentioned in the below blog, we can modify screen resolution during selenium test runs.
http://blog.testingbot.com/2013/03/15/screen-resolution-option-now-available-for-all-selenium-tests
Tried the below code(as mentioned in "https://saucelabs.com/docs/additional-config"), but not setting the specified resolution. Is this still not available for Selenium?
DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability("screen-resolution","1280x1024");

Sauce Labs != Selenium
Sauce labs use that capability to provision you a VM with the desired resolution, it's not a capability that Selenium itself knows about.
Selenium is not capable of modifying your desktop resolution!
If you want to modify your browser size in Selenium so that it matches a specific resolution you can do a:
driver.manage().window().setSize(new Dimension(1024, 768))
The above is not supported with Opera driver, so instead you would need to do:
DesiredCapabilities capabilities = DesiredCapabilities.opera()
capabilities.setCapability("opera.arguments", "-screenwidth 1024 -screenheight 768")
While setting the browser size is not the same as setting the screen resolution, it should for all intents and purposes meet your requirements.

Selenium is a browser automation framework, its job is to drive a browser, not to automate your system. I don't think that a functionality such as setting a screen resolution will ever be implemented in Selenium. Setting resolution has simply nothing to do with browser automation.
I am not sure why do you want to change the resolution... How about just changing the size of your browser window? That's something you could do if you are testing responsive-designed pages.
driver.manage().window().setSize(new Dimension(800, 600));

The purpose of DesiredCapabilities is to tell the Grid where to run your tests.
So, if there is a remote node connected to the Grid with the resolution you specified 1280x1024, the test will run on that node. If any other nodes do not have this resolution, the test will not run on those nodes.
If you do not specify a screen resolution in the DesiredCapabilities, the test will run on nodes with any resolution.
This feature of Selenium does not actually change or modify a testing node's screen resolution. It only tells the Grid on which nodes to run or not run your tests.

What you are interested in doing is probably best done through native java code rather than through Selenium which is a web browser testing framework.
The question of changing the resolution through Java was addressed in another thread here on StackOverflow.
swing - Change screen resolution in Java
Although I am curious as to why you are trying to do this, since you didn't explain that above and may lead to a better response from the community. =)

Related

Java Selenium Chromedriver webdriver as fast as possible

I am connecting a standalone program to a website, and I have to read some pages of the website. For first, I used Jsoup, but with this I discovered that some informations that I need are loaded after page load, so I searched for webdrivers. (I am not looking for images or something big, my content are all textual)
Now, i found the ChromeDriver, but it is too slow for my case because it has a lot of options and features.
In my case I need just a step more than the Jsoup possibilities.
It is possible to disable the best part of ChromeDriver options and features to reach this goal?
For example, i saw plugin disabling, but is one by one and is not for every chrome browser on every pc. I didn't found an option like "plugin.disable-all".
Furthermore,in this way I cannot open more than a few instance of chromedriver. In this moment, every instance of the chromedriver is opening a Google Chrome Helper that uses 100Mb of ram.
Hope all is clear
HtmlUnit might be enough for your needs. It does support some Javascript.
It can be used with Webdriver. But might as well be enough on its own
To make your webdriver run faster (but not that much faster), you can run the driver in Headless mode. See these articles for a tutorial on how to go into Headless mode for Chrome.
Before starting the driver, add the --headless argument to ChromeOptions.
Headless mode can speed up your automation by not rendering the browser window, but keep in mind that doing a straight HTTP GET with JSoup would always be faster.
My advice would be to reverse engineer the page a bit more, and see if you can figure out how to query directly whatever the (presumably AJAX) calls are putting on the page. If you can treat those specific requests as an API and only query for exactly what you want, you will be able to get results faster than with browser automation through Selenium.

Selenium Chrome Driver Limitations Web Scraping at Scale

I'm planning to use Selenium Chrome Driver for my project which will be used to do web scraping to multiple public websites (something like kayak or skyscanner). So there will be a REST GET endpoint where my backend would launch headless Chrome to scrape multiple websites, and eventually return a manipulated JSON.
I want to know how scalable is Chrome Driver as it sounds like a headless Chrome instance needs to be launched whenever a request comes in.
Updated: Question using Google Chrome Headless
Please find the pros and cons of phantom js which I noticed during implementation .Hope this helps.
Cons:
1)It will fail to recognize the browser elements like id,xpath,csselector
when compared to chrome driver.
2)If you have login mechanism ,redirects won't work as you expect when compared to chrome driver.
3)You need to manually implement the custom logic for screen shots for the test failures if you need it.
4)If you want to switch between multiple drivers like chrome,html etc then it is very difficult
Pros:
1)Test case execution is faster when compared to chrome driver
2)No browser is required it will run without GUI.
3)No much configurations are needed when compared to chromedriver.
You can go with html driver also which is quite faster then phantom but even it has its own limitations that you need take care of before implementation.
I am not sure that you really need to use PhantomJS.
Chrome implemented "headless" mode couple of months ago.
"Headless Chrome" does the same job that PhantomJS, and does it better.
I heard that PhantomJS authors even said that they will not support it anymore.
You can enable headless mode in Selenide with just on line:
Configuration.headless = true;
Did you think about headless chrome?
Headless Chrome

Jenkins opens Chrome with limited dimensions

I am using Selenium script (in Java) with Jenkins using Chrome browser.
But Jenkins opens Chrome browser with dimension 1040x784, although I tried to increase dimension as desktop browser but failed, it still opens browser with dimensions 1040x784.
I am using this code to increase dimension:
Dimension dim=new Dimension(1340, 744);
driver.manage().window().setSize(dim);
So in order to open chrome browser with desktop dimension, do I need to add any plugin? Due to this I am not able to automate most of the features with Jenkins.
* I am using Jenkins as my Windows services.
I use the same code as you with jenkins without problem: windows is set to its new size as expected.
I think the issue might be that Chrome does not allow to set the size larger than the dimension of the system's screen.
If you know for sure that your system's screen allows you to reach [1340, 744], then it might be because your chrome driver is not an up to date version.
If it still does not work, below are some tests you might try, to fix the issue:
Try to run it with firefox webdriver, and see if you have the same issue. this will discard an issue related to chrome webdriver.
Try to set new dimension, but smaller than the 1040, 784, and see if chrome lets you do that. It will discard an issue related to the way you have written your code.
does it work fine when you execute it on local ?

Confusion about Selenium and WebDrivers

I got into a project which requires a WebGUI to be tested. Selenium is used for this in combination with Jenkins to schedule tasks and run the tests. All of this is handled by one server (Linux).
Regular tests work fine but WebGUI tests fail and I am supposed to make them work. However I'm very confused by the roles of Jenkins' Plugin "Selenium Grid" (is it even neccessary), the JUnit tests inside the project utilizing a selenium libary and the setup of the headless browser required.
What exactly are the roles of the plugin "Selenium Grid" for Jenkins and how does the Selenium libary communicate with a headless browser (Role of WebDrivers?)?
I already know about the Selenium hub and nodes but have no idea how to approach this.
I hope somebody can help me cleaning up my confusion...
A Selenium Grid can be used to set up a scalable automated browser environment. It is often used for automated GUI testing. It consists of a Hub and one or more Nodes.
A Selenium Hub should be started and the resulting URL should be stored.
Then a desired number of Selenium Nodes should be started with a reference to the Url of the Hub. The nodes will register themselves to the Hub.
Then you could use Selenium Java to create a RemoteWebDriver instance, optionally specifying the url of the Selenium Hub. This webdriver is just a Java API, the actual work will be done in the Selenium Node environment(s).
You can use the Java API to implement your desired operations on the WebDriver instance. When running, the code will delegate all calls to the running Selenium environment.
There are multiple ways to actually start the hub and nodes. A Jenkins Plugin could do it, you could use Docker, a local process, or many more. I'd advise to not bind your setup directly to Jenkins unless you are certain that you will never switch.
The Jenkins Selenium Plugin specifies the URL that you should create a WebDriver with:
new RemoteWebDriver(new URL("http://jenkins.mydomain:4444/wd/hub"), capability);
Where jenkins.mydomain will probably be the IP of your Linux server. Just above it is a line that says that it "also" accepts external nodes. I am not familiar with the plugin, so I dont know whether this means that it sets up one or more nodes itself by default.
taken from the following website.
https://wiki.jenkins.io/display/JENKINS/Selenium+Plugin
This plugin sets up Selenium Grid in the following way
On master, Selenium Grid Hub is started on port 4444, unless configured otherwise in Jenkins global configurations. This is where all your tests should connect to.
For each slave, necessary binaries are copied and Selenium RCs are started.
RCs and the Selenium Grid Hub are hooked up together automatically.
Now the question is do you need this?
This is upto the requirement of the project. You just need to ask this question to yourself
do you need to run tests in parallel (to cutdown time or for browser compatibility testing), either now or in near future.

Error: When i minimize Internet Explorer which is running the automated WebDriver Code

I have written the Selenium webdriver java code to automate the test and its working fine. But I have lot of data input to test my web and it takes time. So when i minimize the IE to do some other task while it is running the automation, it is throwing error:
org.openqa.selenium.ElementNotVisibleException: Element is not displayed
Selenium WebDriver is trying to simulate "real" users interaction with the webpage. If a person can't click on a button not currently displayed, neither can Selenium.
ElementNotVisibleException occurs when the element you want to interact with is not displayed. When you minimize the browser some of the elements are no longer visible, even though they where in maximized window.
You can add scroll using moveToElement() from Actions class every time you want to perform any action (I don't recommend it, you increase significantly the chance for errors), or find another hardware solution, like plugging in another screen, run the test on another computer etc.
According to my experience, the Internet Explorer WebDriver is very oversensitive when it comes to disturbances from a real user while running test cases. It's better to not touch anything at all. ;-)
Try Chrome! This is much more robust and also faster.
Selenium script runs as a simulator. You cannot do another work when script is running. Chrome is fast but while running script in chrome you can not do other task like any other browser. If you minimize window, you will get exception "ElementNotVisible".

Categories

Resources