Is it possible to automatically open Google Chrome devtools? - selenium

Given that Selenium 2 closes the devtools window which contains my emulator profile saved under my user profile for chrome. Is there a way to trigger devtools to open using a selenium script?
Here is the info on the devtools window closing issue
https://sites.google.com/a/chromium.org/chromedriver/help/devtools-window-keeps-closing
I feel a little exhausted trying some of these Chromium override parameters only one of which seems to work
http://peter.sh/experiments/chromium-command-line-switches/
The one that had any affect is the following
options.addArguments("user-data-dir=/Path/to/chrome/profile");
If there is no way to open the dev tools window or panel, is there a way to initialize the emulator?

at the terminal, type the following -
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --auto-open-devtools-for-tabs

You can use Robot class for same. It just going to help you to open dev-tools on any browser.
Use this code on the place you want to open dev-tools
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.VK_F12);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

You can used below given code, it is working fine with FireFox just change the browser
public void open(String url) {
driver.get(url);
Actions action = new Actions(driver);
String pressF12=Keys.chord(Keys.F12,"");
driver.findElement(By.id("lst-ib")).sendKeys(pressF12);
}

As the same link you provided says, it's not possible to use ChromeDev tools with Selenium's ChromeDriver since 2.x. It's simply in direct conflict, you could use either one or the other.
The only way to perform a workaround is to pause all chrome driver interactivity, launch the DevTool's through some other automation framework such as Robots API, do whatever you need, and continue.
But I might think on what I need from dev tools, isn't there an alternative to what you need by providing Chrome with the proper configuration on launch? (loading a different profile, or loading an emulated device)

Why do you need the chrome devtools anyway?
Is it for monitoring the network traffic? Why not usefirefoxdriver with firebug.xpi or browsermobproxy.
To open a page in emulated device's browser. This can be done with chrome, without opening the devtools.
WebDriver with chrome emulated browser
This is a code snippet that you can use to fire up chrome browser in a particular emulate version. Note the key thing here is the deviceName value, which should match as it is mentioned in the chrome browser of yours.
public static WebDriver MobileOpen()
{
Map<String, String> mobileEmulation = new HashMap<String, String>();
System.setProperty("webdriver.chrome.driver","path/to/chromedriver");
mobileEmulation.put("deviceName", "Google Nexus 6");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
return driver;
}
Hope this helps.
Edit: I just saw it is a 3 years old post. The guy probably has found his way home. :P

Have you tried the console API for your needs? It will allow you to invoke some dev-tool functions from inside JS.

Related

Selenium - Automate Chrome Browser Extension - Lighthouse

I am trying to integrate Lighthouse in my Selenium scripts
Basically each time my script lands on new page I should click on the Lighthouse extension and generate a report.
What I could Achieve: (sample working code at the end)
Setting up desired capabilities to launch Chrome with Lighthouse extension added
What I am unable to do:
I want to use the added
While on the desired web page
Click on Chrome Extension button and select Lighthouse extension
In the Lighthouse extension pop up displayed Click on "Generate Report" button
Sample working code:
System.setProperty("webdriver.chrome.driver","C:\\Users\\v744536\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.addExtensions(new File("C:\\Users\\v744536\\eclipse-workspace\\AccessibilityProject\\LH.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities ();
capabilities.setCapability(ChromeOptions.CAPABILITY, opt);
#SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");
I should tell you that the Lighthouse browser extension simply calls the PSI API, which is free and easy to use directly: https://developers.google.com/speed/docs/insights/v5/get-started
The extension directs you to a page, and that page will use the PSI API. If you don't want to use the PSI API directly, another option is to just open the Lighthouse Viewer with the url you want:
https://googlechrome.github.io/lighthouse/viewer/?psiurl=https://www.example.com
That's all the extension is doing under the hood. This would be much simpler than via scripting user interactions.
Source: I work on Lighthouse.

Selenium grid 4: How to run a test with differents browsers and OS

Beginner on selenium grid, I just created a small test that allows to navigate on the home page of stackoverflow
For this I placed the selenium server on my disk, I opened 2 terminals as shown in the documentation here.
https://www.selenium.dev/documentation/grid/getting_started/#hub-and-nodes
The test starts well, and now I would like to run the same test under firefox while modifying the OS (for example: windows). How do I do this? Do I have to create another test file in my project? And then how to run the test with multiple configurations? I can’t find an answer to these questions.
My configuration:
-Linux Ubuntu 20.04
-Google and chrome 95
-the last version selenium server : 4.1.1
Here's the code of the test:
public class StepGoStackoverflow {
RemoteWebDriver driver;
String nodeUrl;
#Given("I'm on google search page")
public void i_m_on_google_search_page() {
try {
nodeUrl = "http://localhost:4444";
ChromeOptions options = new ChromeOptions();
// options.addArguments("--headless");
options.addArguments("start-maximized");
driver = new RemoteWebDriver(new URL(nodeUrl), options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("https://www.google.com");
} catch (MalformedURLException error) {
error.printStackTrace();
}
}
#When("I enter the name of the site")
public void i_enter_the_name_of_the_site() {
WebElement webElementList = driver.findElement(By.id("L2AGLb"));
webElementList.click();
driver.findElement(By.name("q")).sendKeys("stackoverflow", Keys.ENTER);
}
#Then("I'm navigated on the home page of stackoverflow")
public void i_m_navigated_on_the_home_page_of_stackoverflow() {
driver.findElement(By.xpath("//a[#href='https://stackoverflow.com/']")).click();
driver.close();
}
}
EDIT:
I forgot to give the snipet of Gerkhin:
Feature: search the home page of stackoverflow
Scenario: Go to the site stackoverflow
Given I'm on google search page
When I enter the name of the site
Then I'm navigated on the home page of stackoverflow
Thank you
I can see that you are using Cucumber so I edited your question to add corresponding tag (since that matters as we're taking about parameterization).
Grid part
The idea of the Selenium Grid is that you install Grid components at a cluster where the nodes represent different OSs and each runtime/OS has one or more drivers and browsers installed.
So you configure nodes so that they know where are drivers installed and how to run browsers (and what browser can each node execute: e.g. Chrome, Firefox) and each node registers at the grid component called hub.
On one side a hub knows which nodes are running at which OS and what browsers they can operate with. On another hand hub acts as remote web driver (it exposes webdriver protocol to external clients aka automated tests).
You can find an overview of the grid here and find configuration flags and aspects here.
Code part
On the code side you parameterize your tests so that each test is executed from scratch but with different capabilities set up. In your example you only use ChromeOptions which would make hub to dispatch your calls to a node that can run Chrome browser.
Your challenge is to make your code configure different capabilities each run so that Grid will look up appropriate node in the cluster for your test (like "ah-ha.. you want to run Firefox on Linux which means I'll dispatch your calls to node C")
Cucumber
Since you are using Cucumber in your example I assume that you tend to incorporate it into final solution. If so, you need to start from learning parameterization practices in cucumber: See here, here and here; setting up context in Cucumber, and sharing the state in Cucumber with Dependency Injection with Guice or PicoContainer.
Combine everything, add a handful of parallelization and you will gain the most value from your framework.

How do you start selenium using Chrome driver and all existing browser cookies?

From what I understand so far, Chrome Driver always starts without any stored browser cookies.
I need the driver start with all the cookies stored by Chrome.
I wonder if there is any way to start the driver with the cookies that are already stored? I'm using C# with .net 4.5.
Yes we can do it by invoking saved chrome profile just like firefox profile. below are steps i noted when i am doing bit back ago
in Java, we can do it by using ChromeOptions and Chrome Profile. In chrome navigate to chrome://version/ It will display profile path and Executable path.
As per my working on this, The profile path is \Local\Google\Chrome\User Data\Profile 3 This is displaying what is displayed when i navigate to chrome://version/ in normal chrome browser. In this profile, i navigated to stackoverflow and saved credentials. So used below code
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("binary", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\poi-3.12\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("user-data-dir=C:\\Users\\murali\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
//WebDriver driver = new ChromeDriver(options);
driver.get("http://stackoverflow.com/");
As per my understanding, i excepted stackoverflow.com page displayed as logged in. but for first time, i am not logged in. so cross checked with chrome://version/ in chrome opened by driver, profile path is displayed as
\Local\Google\Chrome\User Data\Profile 3\Default . then logged manually in that profile it self, which is opened by webdriver and executed gain by closing it.
Finally, page is displayed as logged in. So it may be in java, i hope it will helps you to try in C# .

Selenium drivers don't remember user authentication

The application I'm testing requires a login with the user's Google account. Every time I log in, it displays/requires that I select the 'Allow Access' button as if it doesn't remember that I have already added it to my list of Authorized Access for my Google account. This doesn't happen when I test manually, only when I'm running Selenium. Has anyone come across an issue like this or know of a solution? Thanks in advance.
WebDriver driver = selenium_driver.get(); // using chrome driver
baseUrl = defaults.getProperty("base_url"); // this is set to my localhost
helper.ConnectToURL(baseUrl);
When this started happening, I had been using Selenium 2.28.0--since then, I've updated to 2.31.0 but it's exhibiting the same behavior.
Disclaimer: This is currently not possible according to the ChromeDriver wiki. It states in the "Known Issues" section "Cannot specify a custom profile". (https://code.google.com/p/selenium/wiki/ChromeDriver)
At some point when it is fixed, I would suggest creating or using the default chrome profile that has your authorized access set that your test uses whenever it starts up.
According to the ChromeDriver wiki: "By default, ChromeDriver will create a new temporary profile for each session".
Checkout this post for more in depth information regarding capabilities: http://code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches
I do my work in .NET and Windows; my set up would look something like this:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("start-maximized");
chromeOptions.AddArgument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, chromeOptions);
ChromeDriver chromeDriver= new ChromeDriver(this.Environment.ChromeDriverLocation, chromeOptions);
If you are not limited to using Chrome for your tests you are able to create and use custom profiles using Firefox.

handle JS alert using IE webdriver, Selenium

I need to click on a cancel button for a Java based application.
I am using IE Driver, Eclipse IDE and my application only supports IE.[i am scripting in Java]
Here is the situation,
Login to the application
There is a account session popup[confirmation box][js]
[The alert has the focus, user cannot focus the application]
Click on the cancel button
Now, i have logged in successfully but i am unable to handle the JS Alert window.So i am unable to write further scripts.
Kindly help me out !!!
If you're using Java, and you're using the Selenium WebDriver API, something like the following code should work:
driver.switchTo().alert().dismiss();
Alert handling has not been implemented for every driver, but it should work for IE.
Remember you can always find the Javadocs for the WebDriver API at this link.
To Handle Alert in IE, you need to set the capabalities for IE first:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability("ignoreProtectedModeSettings", true); //added this to ignore protecion mode setting so as to launch IE
driver = new InternetExplorerDriver(capabilities);
driver.get("url");
driver.switchTo().alert().dismiss(); //or
driver.switchTo().alert().Accept(); // accordingly