How to change useragent-string in runtime chromedriver selenium - java

I am developing something that want me to change the useragent.
In start I supply user agent as... (to chromedriver)
options.addArguments("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25");
But then I want to change it to Windows useragent, or just remove this useragent.
How can I achieve this programatically in Selenium Java.

No, you can't change the useragent of an active Browsing Session once you configure the WebDriver instance through an instance of ChromeOptions and addArguments attribute and start an active Browsing Session.
Even if you are able to extract the Session ID, Cookies, User Agent and other Session Attributes from the active Browsing Session still you won't be able to change/edit those attributes as a HOOK to the WebDriver.
To change the User Agent you have to re-configure the WebDriver instance and initiate a new Browsing Session.

As #Debanjan said You can't change user-agent in runtime, but did You try using ModifyHeader plugin, You can setup it to change user-agent but not at already instantiated driver. You can setup during webDriver instantiation and also try with ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/idgpnmonknjnojddfkpgkljpfnnfcklj.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
or
ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");

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);

PAC file proxy setup using Selenium -Browserstack

I need to set my Browser's proxy with Automatic Proxy configuration URL as shown in the screenshot below.
I am trying to achieve this using Selenium and Browserstack as test environment.
Set the proxy as shown below.
Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://pokgsa.ibm.com/gsa/pokgsa/home/j/m/jmit/web/public/proxy.pac");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.PROXY, proxy);
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "63.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "7");
caps.setCapability("resolution", "1366x768");
Tried to set the proxy configuration locally and it works however it does not work on browserstack. I think the proxy is not getting set on the virtual browser.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
profile.setPreference("network.proxy.autoconfig_url", "http://pokgsa.ibm.com/gsa/pokgsa/home/j/m/jmit/web/public/proxy.pac");
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
You need to pass the pac file details using Firefox profile.
Similarly for chrome, you may refer the following link: https://github.com/SeleniumHQ/docker-selenium/wiki/Corporate-Proxies#setting-a-proxy-for-running-chrome
Also please ensure proxies in the pac file do not need machine based authentication/entries since this may not work as your proxies would be required to be authenticated on all browserstack IPs

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

Starting chromedriver with saved passwords enabled

Everytime that I start the Chrome web browser, using chromedriver, it starts up clean with any custom settings disabled. I have a case where I am logged in on a website and I want to access the account to get some information. However, the newly opened browser is not logged into the account anymore. Even when I open a new browser manually I am still logged in on that same page. Is there a way to enable custom settings? Preferably in Java.
You can achieve this by using ChromeOptions as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeDirPath = "provided here a path where you want to custom chrome dir which could be use everty time you launch"
//ensure chromeDirPath exist in dir
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir="+chromeDirPath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("url");
Now it will maintain your custom browser setting at chromeDirPath
Hope it will help you.

How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver?

I am using the Selenium-Firefox-driver and Selenium-Chrome-Driver version 2.0a5 (Web Driver API), and I am trying to test a web app that has BASIC authentication (there is a popup that come up to authenticate the user when I hit whatever page, the popup is not part of the HTML).
Now, I need to a strategy to authenticate the user in Firefox, Chrome and IE (I'm going to import the IE Driver soon).
I was reading in few articles that I can set a Firefox profile for instance..something like:
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("network.http.phishy-userpass-length", 255);
WebDriver driver = new FirefoxDriver(ffProfile);
driver.get("http://username:password#hostname");
but it doesn't seem to work for me. Does anyone have a working solution for those browsers?
I got it to work with Firefox webdriver by the following:
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://user:pwd#google.com");
True, BASIC HTTP authentication is not currently supported but I got it working now for FF and for Chrome.
The code I wrote in the questions works for those drivers. I just tried using FF3.6 as Firefox default browser (installed in Firefox folder) instead of FF4 (not supported yet). For IE, i may try to disable the authentication through Windows Registry.
This page http://code.google.com/p/selenium/issues/detail?id=34 may help.
For more portability, this can be handled by stub API and using Alert.
Example Java code (sample):
import org.openqa.selenium.Alert;
import org.openqa.selenium.security.Credentials;
public void authenticateUsing(Credentials credentials) {
private final Alert alert;
alert.authenticateUsing(credentials);
}
See also: auth_tests.py
Or by sending keys manually like:
SendKeys("user");
SendKeys("{TAB}");
SendKeys("password");
SendKeys("~"); // Enter
See also the following feature request: #453 Portable BASIC Auth at GitHub
Related:
How to send Basic Authentication headers in Selenium? at QA SE
Add this New Firefox Profile on your code
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("myProjectProfile"); //replace "myProjectProfile" with your profile"
WebDriver driver = new FirefoxDriver(myprofile);
Firefox configuration settings
This works fine without prompting any authentication when you do the following settings..
Type "about:config" on your FF url
Now type "Proxy" in the search field
Make sure "signon.autologin.proxy" is set "true" (By default
it is "false")
Load Default/Custom Chrome Profile to run tests using Selenium
WebDriver
Download chromedriver.exe
Extract the chromedriver_win_26.0.1383.0.zip folder and locate .exe file to C:/ folder
Add this Script on your JAVA code
DesiredCapabilities capability = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
capability.setCapability("chrome.switches", Arrays.asList("–disable-extensions"));
capability.setCapability("chrome.binary", "C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default");
driver = new ChromeDriver(capability);
Note: IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.
If you want to enable the http auth in Internet explorer, you have to edit the registry and add this (create keys if they are not present):
in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0
in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0
Close and reopen Internet explorer
If you have a x64 IE, the path is a bit different :
HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
There is a solution for performing authentication with Selenium 1.x by manually setting the HTTP headers at http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium but I don't think this is transferable to Selenium 2, as you don't have access to the headers.
According to the information here 'Basic Authentication support for Selenium 2' was added in Selenium 2 Beta 2 but looking through the source code I can only see it implemented as a way of securing Remote Selenium Servers against anonymous access.
So I think the answer is that BASIC HTTP authentication is not currently supported.
I was not able to use the basic authentication with Selenium 2 and Chrome (Due a bug with Chrome), so I created an extension for Chrome that sends the basic authentication credentials automatically (See https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn).
Multipass extension of Firefox made automation engineers life easy. Through this, we can handle the basic authentication pop-up in any browser using any programing language. PFB the steps:
Open the Firefox browser and download the plug-in
-> https://addons.mozilla.org/en-US/firefox/addon/multipass/
Now go to the below location to get the XPI file that is the 'multipass' executable file for firefox
-> C:\Users\Your user name\AppData\Roaming\Mozilla\Firefox\Profiles\oleovwxr.extensionUser\extensions
Copy the file 'multipass#gilles.crettenand.info.xpi' from the above directory and past it to your project directory inside any folder of the resource package.
Now use the below code snippet to configure the Firefox driver.
public WebDriver config() {
System.setProperty("webdriver.gecko.driver", "Path to geco driver");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "firefox");
capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WINDOWS");
capabilities.setCapability("acceptSslCerts", true);
capabilities.setCapability("marionette", true);
FirefoxProfile profile = new FirefoxProfile();
//Give the multipass path
profile.addExtension(new File("c:/your project name/src/main/resources/multipass#gilles.crettenand.info.xpi"));
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
firefoxOptions.merge(capabilities);
return new FirefoxDriver(firefoxOptions);
}
Now the main challenge is to get the UUID of the downloaded multipass extension as whenever we run it changes. So we are taking every time when run.
public void setup() throws InterruptedException {
WebDriver driver = config();
driver.get("about:debugging#/runtime/this-firefox");
Thread.sleep(4000);
String uuid = driver.findElement(By.xpath("//span[#title='MultiPass for HTTP basic authentication']/parent::li/section/dl/div/dt[contains(text(),'UUID')]/parent::div/dd")).getText();
System.out.println("My Url:::" + "moz-extension://" + uuid + "/popin.html");
driver.get("moz-extension://" + uuid + "/popin.html");
//change below URL with your URL and username and password
driver.findElement(By.id("url")).sendKeys("http://mywebsite.com");
driver.findElement(By.id("username")).sendKeys("site user name");
driver.findElement(By.id("password")).sendKeys("site password");
driver.findElement(By.xpath("//button[.='Add']")).click();
//Now change below URL with your url, note:: the domain should math with above multipass url
driver.get("http://mywebsite.com/homeLogin.html");
}
I have tested with the below selenium version selenium-java 4.1.1 and selenium-server 3.141.59.

Categories

Resources