Protected by Copyscape
Powered By Blogger

Thursday, April 1, 2021

Is it possible to do the execution in already opened Browser using Selenium API? Why it's required?

Yes, it's possible to do the execution in the already opened browser but your browser should support Remote Debugging. In case of Chrome it's supports remote debugging feature if you have Chrome version 63 or above is available in the System. 

Why it's required?

When we design scripts for Automation Scenarios then there is always a need of debugging the code to fix the issues if any issues found by the script during the run like Object description needs to be updated , Sync issues etc. After fixing such issues we have to re-run the entire script to validate the fix which is a time consuming process.

But if we manually navigate to the page where the fix is required and then verify the fix by running few lines of code using Selenium API on the existing opened browser. So, by doing this way we will get rid of re-run the automation script again and again to validate the fix. To do the same following steps needs to be done.

1. Remote Debugging feature must be configured for Chrome browser by using the following steps:

    a. Try to access your Chrome browser through Command Prompt by typing Chrome.exe command in CMD. If you are unable to access Chrome browser using CMD then you have to set the path or reference for Chrome.exe in System Environment Variables. In my system, chrome.exe is located at 

"C:\Program Files\Google\Chrome\Application". 

So, the same you need to locate in your system and set the path or reference in System Environment Variables. Now try to access the same by typing "Chrome.exe" in CMD.



     b. Now run the following command in the command prompt to configure the Chrome for Remote Debugging purpose.

            Chrome.exe -remote-debugging-port=2323 --user-data-dir="C:/Selenium/Chrome_Test_Profile"



2. Now we have to use ChromeOptions class to do the necessary setup for the same.

System.setProperty("webdriver.chrome.driver","c:/Browser Executables/chromedriver.exe");
ChromeOptions opt=new ChromeOptions();

opt.setExperimentalOption("debuggerAddress","localhost:2323");
WebDriver driver;
driver=new ChromeDriver(opt);

Only the above 2 steps are required to perform to do the execution on the existing browser.

The following example you can try to practice the same.

1. Open facebook.com in the browser which you just opened by using the command which is mentioned in the above point 1 (b).

2. Click on Forgot Password link and then execute the following code to do the execution in the same browser which is already opened on port 2323.

package com.company;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Main {

static WebDriver driver;

public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver","c:/Browser Executables/chromedriver.exe");
ChromeOptions opt=new ChromeOptions();

opt.setExperimentalOption("debuggerAddress","localhost:9014");
driver=new ChromeDriver(opt);

driver.findElement(By.xpath("//*[@id='identify_email']")).sendKeys("7011516489");

}
}
***********************************************************************************************************************************************************

I will be adding more concepts in my upcoming posts. Please keep me posted if you have any questions and concerns.



Best of Luck.

No comments: