I'm using Selenium Web Driver on Windows 7.
I'm trying to test a website that use authentication and I need to use SSL certificates.
When I use Firefox out of Selenium all works fine but I've noted that the Firefox browser session opened by Selenium doesn't have any certificates registered and so it's clear that it doesn't work.
Here you are the Advanced Preferences when I use Firefox "out" of Selenium
and here you are the same when I use the Firefox sessione opened by Selenium
I've tried to keep the session opened and to register manually the certificate but the browser session doesn't register the certificate.
Here you are my code if could be useful
package myTestProjects;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GAMOPERA_Test_01 {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
System.out.println("Creo una nuova sessione del browser Firefox ...");
driver = new FirefoxDriver();
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// It is always advisable to Maximize the window before performing DragNDrop action
System.out.println("Massimizzo la finestra del browser ...");
driver.manage().window().maximize();
Thread.sleep(3000L);
//Launch the Sistema Piemonte Home Page
System.out.println("Mi collego a Sistema Piemonte ...");
driver.get("http://<my_site_url>");
Thread.sleep(3000L);
// Find the element Accedi o
System.out.println("Accesso tramite certificato digitale ...");
driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();
//driver.findElement(By.className("loginbutton")).click();
Thread.sleep(3000L);
// Print TEST = OK!!
System.out.println("TEST = OK !!");
//driver.quit();
}
}
Any suggestions?
I've solved!
Surfing on the web I've found this post http://seleniummonk.blogspot.it/p/how-to-handle-ssl-cerificates.html that gave me the solution.
I need to use the "Firefox profile" (I use the default one ...), so I can have all the certificates I need to.
Here you're the new code that works
package myTestProjects;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class GAMOPERA_Test_01 {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("default");
// Create a new instance of the Firefox driver
System.out.println("Creo una nuova sessione del browser Firefox ...");
driver = new FirefoxDriver(ffProfile);
//Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// It is always advisable to Maximize the window before performing DragNDrop action
System.out.println("Massimizzo la finestra del browser ...");
driver.manage().window().maximize();
Thread.sleep(3000L);
//Launch the Sistema Piemonte Home Page
System.out.println("Mi collego a Sistema Piemonte ...");
driver.get("<my_site_url>");
Thread.sleep(3000L);
// Find the element Accedi o
System.out.println("Accesso tramite certificato digitale ...");
driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();
//driver.findElement(By.className("loginbutton")).click();
Thread.sleep(3000L);
// Print TEST = OK!!
System.out.println("TEST = OK !!");
//driver.quit();
}
}
I hope this could be useful!
You can do that using Proxy. Through DesiredCapabilities you can configure the browser accordingly.
String PROXY = "localhost:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new InternetExplorerDriver(cap);
Code taken from SeleniumHQ
First create a profile as I created with "Test" in Firefox using below command in CMD:
"C:\Program Files\Mozilla Firefox\firefox.exe" -P
Then import your certificate in this newly created Firefox Profile with Password used to generate the certificate.
In my case this was a P12 extenstion Certificate.
Once done you can use below code in Selenium and Firefox will not popup for Certificate and you will be logged into the Portal or Website.
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile myProfile = allProfiles.getProfile("Test");
myProfile.setPreference("security.default_personal_cert", "Select Automatically");
FirefoxOptions firefoxoptions = new FirefoxOptions();
firefoxoptions.setProfile(myProfile);
WebDriver driver = new FirefoxDriver(firefoxoptions);
Background I am using Firefox 56.0 and Windows 7
Related
I want to open a URL in the Chrome browser, but in mobile mode during my load test in JMeter. I am using Selenium scripts. Below is my Selenium script
var pkg = JavaImporter(org.openqa.selenium,org.openqa.selenium.support.ui) // Import Java Selenium packages
var Thr = JavaImporter(java.lang.Thread) // Import Thread sleep packages
var wait = new pkg.WebDriverWait(WDS.browser,30) // Import WebDriverWait Package
WDS.sampleResult.sampleStart()
WDS.browser.get('https://xyz=${__urlencode(${token})}');
WDS.sampleResult.sampleEnd()
Below is the Java class I have created in BeanShell Preprocessor in Jmeter to use Chromeoptions to open Chrome in mobile mode, but I don't know how I can call it in webdriver above and I am doing it correctly or not:
public class page {
public static void main (String args[]) {
String device = "Samsung Galaxy S4";
//options.ChromeOptions options = new ChromeOptions();
ChromeOptions options = new ChromeOptions();
options.EnableMobileEmulation(device);
IWebDriver driver = new ChromeDriver(options);
}
}
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language
There is no EnableMobileEmulation function in ChromeOptions, you should be using setExperimentalOption instead
Example code for the JSR223 Sampler:
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver.exe");
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Galaxy S5");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("mobileEmulation", mobileEmulation);
ChromeDriver driver = new ChromeDriver(options)
driver.get("http://example.com");
log.info(driver.getTitle());
driver.quit();
I have:
Selenium Firefox WebDriver v.3.8.1
Browser Firefox 43
XPI-file with firefox add-on
I ran the extension in the browser in two ways: jpm and using the program on the java through selenium firefox web-driver.
In the first case, I run command jpm run, which creates a new profile with the extension installed and running. It is important that the extension is automatically launched immediately after opening the browser.
I need to achieve the same result, but with the help of the selenium webdriver. As a result of my program, a profile is created with the extension installed, but the extension does not start the same way as when executing the jpm run command.
Help, please, understand what can be the problem.
My code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxBinary;
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
public class MyClass extends Thread {
private String baseUrl;
public MyClass(String baseUrl) {
this.baseUrl = baseUrl;
}
public void run() {
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("C:\\switcher.xpi"));
profile.setPreference("extensions.#switcher.sdk.load.command", "run");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(caps);
driver.get(this.baseUrl);
}
public static void main( String[] args){
System.setProperty("webdriver.firefox.marionette",
"C:\\geckodriver.exe");
Thread t1 = new MyClass("http://google.com");
t1.start();
}
}
P.S. I tried to install the firebug with the help of a selenium webdriver - the problem is the same.
I had the same issue as you. Desired capabilities are deprecated. The way to go is FirefoxOptions.
private void initializeFirefoxDriver() {
//set the location to your gecho driver
System.setProperty("webdriver.gecko.driver", USER_DIRECTORY.concat("\\drivers\\geckodriver.exe"));
//instantiate the Firefox profile
FirefoxProfile profile = new FirefoxProfile();
//Adding the location to the extension you would like to use
profile.addExtension(new File("Path_T0_Your_Saved_Extention\\try_xpath-1.3.4-an+fx.xpi"));
//Setting the preference in which firefox will launch with.
profile.setPreference("permissions.default.image", 2);
//instantiating firefox options.
FirefoxOptions options = new FirefoxOptions();
//Passing the profile into the options object because nothing can ever be easy.
options.setProfile(profile);
//passing the options into the Firefox driver.
webDriver = new FirefoxDriver(options);
webDriver.manage().window().maximize();
webDriverWait = new WebDriverWait(webDriver, GLOBAL_TIMEOUT);
}
The code is not complete and just a snippet, but that will start an extension of your choice. Firebug is no more as well so maybe look at TruePath and Find Path extensions. H
Hope this helped.
I intend to perform some tests by using selenium with multiple web browsers. To distinguish between the different web drivers, I use the following line of code:
((RemoteWebDriver) driver).getCapabilities().getBrowserName();
This will return a String indicating the web browser that is used by the driver object. However, for my Opera WebDriver object this will give me the String 'chrome'. I have tried changing this by explicitly setting the browser name to 'opera' using DesiredCapabilities:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("opera");
WebDriver driver = new OperaDriver(capabilities);
Unfortunately, this does not fix my problem. How do I effectively change the web browser name?
Your basic requirement is to identify browser initialization if I'm right, which can be done by getting user-agent from your browser using JavascriptExecutor as following:
String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
//following is for identifying opera browser initialization
if(userAgent.contains("OPR/"){
System.out.println("Browser currently in use is Opera");
}
Similarly you can identify other browser initilisation by referring this link
Unfortunately , you would not be able to change the BrowserName.
What instead you can try is to create function for specifically handling multiple browsers: -
package multiBrowser;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.Parameters;
public class MultiBrowserClass {
WebDriver driver;
#Test
#Parameters("browser")
public void multiBrowsers(String browserName) throws InterruptedException{
if(browserName.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
driver = new FirefoxDriver(myprofile);
}
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if(browserName.equalsIgnoreCase("opera")){
System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe");
driver = new OperaDriver();
}
driver.manage().window().maximize();
driver.navigate().to("https://");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/a")).click();
driver.findElement(By.xpath("//div[#id='navbar-main']/ul/li[5]/ul/li/a")).click();
Thread.sleep(3000);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("abc#mm.kk");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("1qaz2wsx");
Thread.sleep(3000);
driver.findElement(By.xpath("//form[#id='loginform']/div[8]/button")).click();
Thread.sleep(5000);
if(driver.getPageSource().contains("Welcome abc#mm.kk")){
System.out.println("User Successfully logged in");
}else{
System.out.println("Username or password you entered is incorrect");
}
driver.quit();
}
}
=======
I am trying to open a new tab in the browser.
But however it open the second URL in the same tab.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeFlock {
public static void main(String[] args) throws Exception { WebDriver driver; System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver();
driver.manage().window().maximize();
String baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.get("http://www.facebook.com"); }
}
use JavascriptExecutor as following:
((JavascriptExecutor) driver).executeScript("window.open('http://www.facebook.com');");
Perhaps you are not switching to the new tab, which is resulting in launching the 2nd link on the parent tab only.
You can use Robot class to open a new tab by simulating pressing of keyboard's Ctrl+t keys. Then you need to switch to the new tab using driver.switchTo() command.
For code snippet and details check this Open a new tab in Selenium
You could use keyboard emulation:
new Actions(driver).sendKeys(Keys.Control + 'w').build.perform(); // or + 't'
driver.get("http://www.facebook.com");
or using JavaScriptExecutor:
((JavascriptExecutor) driver).ExecuteScript("window.open('http://www.facebook.com','_blank');");
I am trying to capture https traffic using selenium but is not able to capture it. Http traffic is captured correctly but having issue with https traffic. Get the same message on both chrome and firefox
'Oops.
Something went wrong.
Firefox can't load this page for some reason.'
package com.quadanalytix.selenium;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Test2 {
public static void main(String[] args) throws Exception {
ProxyServer server = new ProxyServer(4446);
server.start();
// get the Selenium proxy object
Proxy proxy = new Proxy();
proxy.setSslProxy("localhost:4446");
proxy.setHttpProxy("localhost:4446");
//captures the moouse movements and navigations
server.setCaptureHeaders(true);
server.setCaptureContent(true);
// configure it as a desired capability
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, proxy);
System.setProperty("webdriver.chrome.driver", "/Users/tarunaggarwal/Desktop/Selenium/ChromeDriver");
// start the browser up
WebDriver driver = new ChromeDriver(capabilities);
server.newHar("gmail");
driver.get("https://www.gmail.com/");
server.stop();
driver.quit();
}
}
Why are you creating a new Java proxy object instead of the follwing?
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
Can you try the following with chrome:
ChromeOptions options = new ChromeOptions()
options.addArguments("--proxy-server=localhost:4446")
driver = new ChromeDriver(options)