Waiting in Selenium tests after Create webdriver in current thread - java

I have been question about selenium test with chrome headless variant.
Sometimes test is waiting anything, why is it has long time 10 minutes?
I added into tests timeout 40 sec.
15:41:54 INFO: Selenium WebDriver v. 3.14.0 build time: 2018-08-02T20:19:58.91Z
15:41:54 Oct 12, 2018 12:41:55 PM com.codeborne.selenide.impl.WebDriverThreadLocalContainer createDriver
15:41:54 INFO: Create webdriver in current thread 1: ChromeDriver -> ChromeDriver: chrome on LINUX (6332d1a2dee8e95f05da4130b99237f9)
15:51:54 [34mEmbed Failed timeout
15:51:54 (Session info: chrome=69.0.3497.100)
15:51:54 (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 3.10.0-862.14.4.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
15:51:54 Command duration or timeout: 0 milliseconds
Selenium settings:
case "chrome":
testBrowserName = "CH";
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1080");
ChromeDriver driver = new ChromeDriver(chromeOptions);
WebDriverRunner.setWebDriver(driver);
break;

You havn't mentioned about your usecase in details and it is not clear why you would use restrict your tests to ChromeDriver implementation only.
If you use ChromeDriver driver = new ChromeDriver(); the ChromeDriver instance which will get created will be only able to invoke and act on the methods implemented by ChromeDriver and supported by Chrome Browser only. To act with other browsers you have to specifically create individual objects as below :
FirefoxDriver driver = new FirefoxDriver();
InternetExplorerDriver driver = new InternetExplorerDriver();
WebDriver Interface
From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla, Chrome, Internet Explorer, Safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available Browsers without any change.
You will find a detailed discussion in what is the difference between ChromeDriver and WebDriver in selenium?
Solution
Instead of using the ChromeDriver implementation switch to the WebDriver Interface. As per current scenario, we have to instantiate the implementations of WebDriver Interface directly. The current practice is, we need to write our Automated Test Script against this interface so that in future we may swap in a more fully featured Browser when there is a requirement for one.
Example:
case "chrome":
testBrowserName = "CH";
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920,1080");
WebDriver driver = new ChromeDriver(chromeOptions);
WebDriverRunner.setWebDriver(driver);
break;

Related

Change proxy after starting chromedriver in java selenium

Is it possible to use proxy after starting chromedriver on the same browser?
EX:
I start chrome driver
Load website
Put in info
Use proxy
Click submit
I think i found some ways to do it in python and JS but im not sure how to convert it to java
As per Selenium's current implementation once you configure the WebDriver instance with the required Options and Capabilities and initialize the WebDriver session to open a Web Browser, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.
So, in-order to use a proxy you have to initiate a new WebDriver session.
here is #JimEvans clear and concise comment (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.
You can find a relevant discussion in Set capability on already running selenium webdriver
You can use ChromeOptions class.
You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions
object into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
ChromeOptions options = new ChromeOptions();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);
// Add a ChromeDriver-specific capability.
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);

Creating Firefox profile and switching off the marionette

I am coming from Ruby background, I know how to do this in Ruby Selenium Binding, but I don't know how to do it Java Selenium Binding,
I have this code to create Firefox profile
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
WebDriver driver=new FirefoxDriver(firefoxProfile);
It works in selenium 2.53 but it's throws error in very recent selenium binding 3.11.0, Can anyone tell me what's the alternative?
And also I wanted to switch off the marionette to connect to Legacy Firefox driver, I can do this with the following code
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", false);
WebDriver driver=new FirefoxDriver(capabilities);
But if I use the above line, then it gives the FirefoxDriver is deprecated. Can anyone guide me how to create profile as well as how to switch off the marionette?
Yes FirefoxDriver(desiredCapabilities) is deprecated.
Alternate way would be to go with options:
FirefoxOptions foptions = new FirefoxOptions(capabilities);
WebDriver driver=new FirefoxDriver(foptions);
Update : [In order]
FirefoxOptions foptions = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
foptions.setProfile(firefoxProfile);
foptions.setCapability("marionette", false);
foptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
WebDriver driver = new FirefoxDriver(foptions);
To use an existing Firefox Profile for your Test Execution first you have to create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows. Now you have to pass the Firefox Profile to a FirefoxOptions class object. Additionally as you would be using the Legacy Firefox Browser
you have to set marionatte to false through a DesiredCapabilities class object which you need to merge() into the FirefoxOptions class object as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(testprofile);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
options.merge(dc);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
Update
I am not sure about your usecase and why you want to use Legacy Firefox Driver. But as per the GitHub discussion Unable to Start Firefox Using the Legacy Driver on a 3.5.3 Grid #jimevans clearly mentions :
The legacy Firefox driver won't work with Firefox 53 or so. You might get the browser to launch, but the language bindings will be entirely unable to communicate with the driver (because Firefox will refuse to load the browser extension that is the legacy Firefox driver).
#barancev also mentions :
A binding should not pass OSS capabilities in W3C-compliant parts of payload, in "capabilities" block. They are allowed in "desiredCapabilities" block only. Perhaps, Mozilla broke Selenium compatibility in Firefox 48 in release channel, but restored it in version 52 in esr channel. It was unexpected, but it's true.
It's all upto you to take a informed descission.

Is there a way to clear session data to ensure a clean session is initiated each time?

For IE you would use capabilities like this:
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
and possibly in combination with
driver.manage().deleteAllCookies();
How could this be achieved using Chrome and ChromeDriver?
While we work with Internet Explorer Driver we use the field IE_ENSURE_CLEAN_SESSION
IE_ENSURE_CLEAN_SESSION
As per the JavaDocs IE_ENSURE_CLEAN_SESSION is the Capability that defines whether to clean or not browser cache before launching Internet Explorer by IEDriverServer and is configured as follows :
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
Now let us have a look at GeckoDriver which follows the WebDriver Spec.
GeckoDriver / moz:profile / rust_mozprofile
If you have a closer look at the geckodriver logs closely you will observe that each time geckodriver is called a new moz:profile is scopped out and the details of rust_mozprofile occurs in the following line:
Marionette CONFIG Matched capabilities: {"browserName":"firefox","browserVersion":"56.0","platformName":"windows_nt","platformVersion":"6.2","pageLoadStrategy":"normal","acceptInsecureCerts":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"rotatable":false,"specificationLevel":0,"moz:processID":5848,"moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d","moz:accessibilityChecks":false,"moz:headless":false}
This log clearly indicates that Marionette scoops out a new "moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d" and this configuration is handled by the WebDriver instance i.e. the GeckoDriver.
You can find a more detailed discussion on moz:profile in Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory discussion.
ChromeDriver
ChromeDriver which is following the same WebDriver Spec does abides (will be abiding) by the same suite.
Incase you are using any stored FirefoxProfile or ChromeProfile, WebDriver will pick up the existing profile where the Stored Browser Configurations are picked up for reuse.
driver.manage().deleteAllCookies();
Irespective of New/Existing FirefoxProfile or ChromeProfile if you add the line :
driver.manage().deleteAllCookies();
Only the cookies gets deleted only to be get restored back to support the Active Browser Session

How to implement the selenium on public browser

WebDriver driver=new FirefoxDriver();
It automatically open the Private firefox browser. Is there any option to open normal browser instead of Private browser.
Not without some serious work on your end. When you create a new WebDriver instance, a firefox instance is started that uses no user profiles. It's like a fresh installation. WebDriver installs a profile on this clean browser instance and installs an extension that runs a little micro web server that listens for instructions from your java program. This little server is what enables two way communication between the browser's javascript environment and your remote java program.
The relationship between the running instance of this firefox web server extension and the WebDriver instance running in your java program is a tightly controlled partnership. It's not part of their criteria to allow you to do what you're asking, so you would have to venture far outside the boundaries of what they support.
You can directly invoke the browser without giving setProperty in firefox for selenium versions <3.0
Firefox in Selenium 3
System.setProperty("webdriver.gecko.driver","path of the driver");
WebDriver driver = new FirefoxDriver();
Replace webdriver.gecko.driver with webdriver.firefox.marionette if above doesn't work.
Chrome browser
System.setProperty("webdriver.chrome.driver", "path of the driver");
WebDriver driver=new ChromeDriver();
IE browser
System.setProperty("webdriver.ie.driver","path of the driver");
WebDriver driver=new InternetExplorerDriver();
Headless Browser
WebDriver driver = new HtmlUnitDriver();

PDF is not being opened in Chrome started by Selenium

I am using Selenium and ChromeDriver 2.43.1 with the latest Chrome (Version 42.0.2311.135 at the time the quetion was asked). My web application generates a PDF. It is being sent with the correct MIME type and it also correctly opens in the Chrome PDF viewer. However when I try to open the PDF using Selenium in Chrome that is started by the WebDriver, it gets downloaded.
I believe it might be some settings that Selenium or WebDriver use to start Chrome.
I've tried settings a few switches, but nothing worked yet. My code:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--please-make-it-work"); // not a real switch
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new ChromeDriver(capabilities);
webDriver.get(url);
What I really need is to start the browser in "normal" mode. It doesn't need any profile settings, just the defaults that will open the PDF.
The problem was caused by the recent change to the behaviour of the --test-type switch. It is described in the ChromeDriver issue tracker.
The workaround is to disable this switch. Here's my code changed:
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
webDriver = new ChromeDriver(options);
webDriver.get(url);

Categories

Resources