How to install plugin while launch the chrome browser? - java

I am using selenium,cucumber and java for automation testing.
I have a requirement of installing a plugin to the chrome browser to open my application. This plugin I have already installed. But when I open the chrome browser through selenium this plugin is missing and application is not working.
How to install the plugin each time when the browser is opening?

I'm using Chrome 65.0 and Selenium 3.11.0
Steps :
Run the following URL in Chrome: chrome://version/
Copy the Chrome profile path.
Open the Chrome profile path in Windows Explorer.
Open the Extensions folder (See the date and timestamp, when you installed that extensions)
Copy the folder path.
Code :
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", "F:\\Automation\\chromedriver.exe");
String pathToExtension = "C:\\Users\\USER_DELL_2014_07\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3\\Extensions\\bhlhnicpbhignbdhedgjhgdocnmhomnp\\2.0_0";
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=" + pathToExtension);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
}
That's it! You can use this code and chrome would be instantiated with the given extension
Please let me know if you have any concerns related to this.

Related

Selenium: Getting chrome didn't shut down correctly

I'm getting the chrome didn't shut down correctly error message when I reopen a chrome browser in my selenium framework.
In the framework I'm opening the browser instance at the beginning for each of my test case using the below code
if (browserType.equalsIgnoreCase("Chrome")) {
try {
System.setProperty("webdriver.chrome.driver", curProj+"\\drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("disable-infobars");
//options.addArguments("user-data-dir=C:/Users/xw20/AppData/Local/Google/Chrome/User Data");
options.addArguments(chromeProfile);
webdriver = new ChromeDriver(options);
logger.info("getWebDriver - Setting webdriver.chrome.driver system property as: " + System.getProperty("webdriver.chrome.driver"));
}
catch(IllegalStateException e) {
logger.error("The path to the driver executable must be set by the webdriver.chrome.driver system property. ",e.fillInStackTrace());
throw new IllegalStateException("The path to the driver executable must be set by the webdriver.chrome.driver system property.");
}
and closing at the end using the below code
driver.close();
driver.quit();
But when I open a browser for the second test case I'm getting "chrome didn't shut down correctly" popup message.
I tried updating the below in the Preferences file of chrome profile but no luck
exit_type:Crashed
exited_cleanly:true
Configuration :
Chrome Version: Version 64.0.3282.186 (Official Build) (32-bit)
Selenium Version: 3.11.0
As per your code it would be tough to analyze the reason behind the error chrome didn't shut down correctly without knowing your framework structure. Perhaps a more details about how code block was invoked (i.e. main() or TestNG) would have helped us.
Having said that there still seems some more factors to look at as follows :
If you are using an existing Chrome Profile through user-data-dir ideally you should avoid the switches setExperimentalOption and addArguments for customization as those should be set within the respective Chrome Profile.
As you are using an existing Chrome Profile through user-data-dir as per the documentation ChromeDriver - WebDriver for Chrome the path should point the profile directory as follows :
options.add_argument("user-data-dir=C:/Users/xw20/AppData/Local/Google/Chrome/User Data/Profile 2")
Here you can find a detailed discussion at How to open a Chrome Profile through Python
Avoid using driver.close(); and always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Here you can find a detailed discussion at PhantomJS web driver stays in memory
Upgrade JDK to recent levels JDK 8u162.
Upgrade Selenium to current levels Version 3.11.0.
Upgrade ChromeDriver to current ChromeDriver v2.37 level.
Upgrade Chrome version to current Chrome v65.x levels.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Execute your #Test.
Did you set exit_type:Normal, I'm currently doing that before the test start, and or after the test ends and it Works.
on C#
public static void FixChromeSingleProfile(string dataDir)
{
FileStream fs = new FileStream(dataDir, FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string json = sr.ReadToEnd();
sr.Close();
fs.Close();
dynamic jsonDe = JsonConvert.DeserializeObject(json);
if (jsonDe.profile.exit_type != "Normal")
{
jsonDe.profile.exit_type = "Normal";
string r = JsonConvert.SerializeObject(jsonDe);
StreamWriter sw = new StreamWriter(dataDir, false);
sw.Write(r);
sw.Close();
}
}
This solution is in Python and works 100% but you should be able to implement this in Java as well. I just call the close_windows function every time I am finished using Selenium.
def close_windows():
windows = driver.window_handles
for w in windows:
driver.switch_to.window(w)
driver.close()
driver.quit()
close_windows()

Java & Selenium : File download automation not working in headless mode with ChromeDriver

I want to download a file from a website in headless mode using Selenium in Java. The code works fine without headless mode and file gets downloaded to downloads folder on the machine. But when I try to use headless mode, the file does not get downloaded somehow. Also, the program does not show any error or exception.
Below is the code. When I comment the statement 'options.addArguments("headless");' , Chrome browser opens the website and clicks the required buttons to download the file. But , I want to use the headless mode only. Please help.
File file = new
File(StackApplicationDownloader.class.getClassLoader().getResource("driver/chromedriver.exe").getFile());
StackApplicationDownloader.class.getClassLoader().getResource("driver/chromedriver.exe").getFile();
String driverPath=file.getAbsolutePath();
System.out.println("Webdriver is in path: "+driverPath);
System.setProperty("webdriver.chrome.driver",driverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://340bopais.hrsa.gov/reports");
driver.findElement(By.xpath("//*[#id=\"headingTwo\"]/h4/a")).click();
driver.findElement(By.xpath("//*[#id=\"ContentPlaceHolder1_lnkCEDailyReport\"]")).click();
System.out.println("Done");

Having issues with firefox driver or chrome driver for selenium

For ex my chrome when dropped in the commpand prompt gives me the path
- /Applications/Google\ Chrome.app
I set
System.setProperty("webdriver.chrome.driver", "/Applications/Google/Chrome.app");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
But it doesnt work, same with firefox. I used a lot of suggestions already given but none seems to work. Can someone pls let me know if there is something to be added?
Why have you used "/Applications/Google/Chrome.app". You would need to provide the path of the driver only, not the browser. Below is the code for Firefox, but you would need to download and configure GeckoDriver (for latest version of FF and Selenium 3.x)
public class FirefoxTest {
#Test
public void FirefoxTest_Test1() {
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Check this link for complete details for downloading and setup of Geckodriver with Firefox - http://automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/
For Chrome: Need to Download fresh Chrome Driver from http://chromedriver.storage.googleapis.com/index.html?path=2.24/
and mention local system path up to chomedriver.exe
System.setProperty("webdriver.chrome.driver","G:\\ravik\\Ravi-Training\\Selenium\\Drivers\\cd\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeDriver d1 = new ChromeDriver(capabilities);
For FF: if your firefox version is latest(46.0 or above) then user geckodriver along with selenium 2.53.0 jar files. download geckodriver form https://github.com/mozilla/geckodriver/releases and then save it as "wires" in your local system. mention local system path up to wires.
System.setProperty("webdriver.gecko.driver", "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe");
WebDriver driver = new MarionetteDriver();
Hope this could be helpful.
The easiest way to use chrome driver is.. download and place the driver into the bin folder of your project. no need to set the path of the driver location

Selenium 3 - Selenium Jar issue error is "The jar file Selenium-server-standalon-3.0.0-beta3.jar has no source attachment"

Not sure why I am seeing this error.
I installed the new java 8_101. Have the jre and jdk present in the machine
Selenium - Eclipse Luna 64bit.
In my ref library I have:
Client-combined-3.0.0-beta3-nodeps.jar
Selenium-server-standalon-3.0.0-beta3.jar (this one has the error)
junit-4.10(1).jar
this was my script below:
/*
System.setProperty("webdriver.gecko.driver", "C://Selenium driver//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
*/
Launches Firefox and Chrome but doesn't go to google.
Bro, try changing this:
System.setProperty("webdriver.firefox.marionette", "C://Selenium driver//geckodriver.exe");

How can I start installed browser without adding IE driver or Chrome driver in selenium web driver?

I have installed IE and chrome browser in my computer. I want to run my selenium script from original browser with all add-ons and default setting.
I am able to find *.exe of browser with some capabilities.But can not able to write and open link (driver.get()) in browser. Please refer following code.
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, DriverTestNG.url);
DesiredCapabilities.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
System.setProperty("webdriver.ie.driver", "src/main/resources/Framework/Drivers/Windows/IEDriverServer_Win32_2.40.0/IEDriverServer.exe");
cap.setCapability("IE.binary", "C:\\Program Files\\Internet Explorer\\iexplore.exe");
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setJavascriptEnabled(true);
cap.setCapability("requireWindowFocus", true);
cap.setCapability("enablePersistentHover", false);
cap.setCapability("elementScrollBehavior", 1);
cap.setCapability("cssSelectorsEnabled", true);
cap.setCapability("nativeEvents", true);
driver = new InternetExplorerDriver(cap);
May be I have missed something.I am not sure about this that selenium web driver supports this functionality or not.
Please guide me for it.
Thanks in advance.
Regarding to your title, you can not run Internet Explorer or Chrome without using a webDriver because you need the webDriver as an API to access the functionality of IE or chrome.
But you can still use extensions and your default settings. The reason why you don't see any extensions running chromeDriver is that it always creates a new temporary profile for each test-session. If you want to run your own custom profile with extensions and settings you have to tell chromeDriver which user profile it should use by defining the user-data-dir.
You can find the capabilities here:
https://sites.google.com/a/chromium.org/chromedriver/capabilities
example:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
You can also specifiy extensions by using:
https://sites.google.com/a/chromium.org/chromedriver/extensions
I don't use IEdriver so I can't tell you how it works with the IE but as far as i know the internet explorer does not have profiles and the extensions are managed somewhere in the registry. So I would assume that extensions installed before running the tests are available also trough IEWebDriver.

Categories

Resources