Selenium - Automate Chrome Browser Extension - Lighthouse - selenium

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.

Related

Chromedriver - IE Tab permission not granted

I am testing web application. In my test I check client data. For use this test I need IE Tab in chromedriver. I'm initializing chromedriver with IETab correctly. After that page is display with below popup
With popup in console I see below log:
Uncaught IETABAPI Error: Permission not granted. You must call window.ietab.requestAccess to use the IE Tab Api.
I clik Allow and nothing's gonna happen. Correctly test should opening new bookmark in chromedriver. Below my code which is executing chromedriver with IETab.
default void ChromeExtensionIETab() {
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("C:\\Users\\user\\Dysk Google\\all\\testowanie\\chromedriver_win32\\extension_12_4_4_1.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.addArguments("--use-fake-ui-for-media-stream");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Dysk Google\\all\\testowanie\\chromedriver_win32\\chromedriver.exe");
setDriver(new ChromeDriver(options));
getDriver().get("https://api-test/");
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(18, TimeUnit.SECONDS);
}
Can someone explain me what should I do?
The Enterprise version of IE Tab includes permissions that would otherwise have to be manually enabled by your end users. Read more here.
If IE Tab is installed:
Click here to allow IE Tab to access https:// URLs and file downloads
Enterprise customers please contact us at support#ietab.net to discover how to deploy these settings automatically.
What we don't see is the web page that you are "driving".
The pop-up you are seeing is from the IE Tab api which must first call window.ietab.requestAccess and wait for the result before calling window.ietab.openWithIETab.
The error you are seeing is because the page called window.ietab.openWithIETab without waiting for a response form requestAccess.
So the web-page is probably calling both calls without waiting for the result from requestAccess, perhaps because the developer had already allowed access so they aren't aware that this pop-up is showing any more.

webGL dependent application in opening in chrome

I have a situation here that we have a webGL dependent application to automate. i have automated the whole application on my laptop, and this application is running fine on chrome on my laptop. but the issue is when i run this application from a seperate machine which is a selenium GRID node. It gives me a message that the application required Webgl and and it ask me to enable the webGL. i have tried different methods like selenium option and ignoring the chrome black list as well.
when i open the browser manually and open the application it runs find and i checked on url chrome://gpu. it shows that the gpu is enaled.
but when i invoke the selenium and run the test cases,. the browser initiates with WebGl as disabled
i found the solution to this. here is the code you can use to enable WebGL for chrome.
DesiredCapabilities capability = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
**options.addArguments("--ignore-gpu-blacklist");**
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability("chrome.binary", "C:\\Temp\\chromedriver.exe");
capability.setVersion("9999");
capability.setCapability(ChromeOptions.CAPABILITY, options);
return new RemoteWebDriver(HUB_URL,capability);

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# .

Is it possible to automatically open Google Chrome devtools?

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.

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