Headless & WebSecurity Capabilities on Browsers

Development & UX Researches
2 min readFeb 4, 2021

--

Question: How can we disable web security of chrome browser using selenium/TestNg during UI testing?

SDET & Updates: WebDriver Code

Once I decide to run my test cases behind the screen without disturbance or running the test cases without security option enabled, I have to come up with the desired capabilities of the browsers. We can push the running windows behind the view and also disable/enable the web-security arguments. Here is the how:

ChromeDriver

String driverPath = “src\\test\\resources\\framework\\testautomation\\commons\\utilities\\driver\\chromedriver.exe”
System.setProperty(“webdriver.chrome.driver”, driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
chromeOptions.addArguments(“ — disable-web-security”);
chromeOptions.addArguments(“ — allow-running-insecure-content”);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
chromeOptions.setHeadless(true);
ChromeDriver driver = new ChromeDriver(capabilities);
//WebDriver driver = new ChromeDriver();
//driver= new ChromeDriver(new ChromeDriverService.Builder().usingPort(34533).build());
WebDriverWait wait=new WebDriverWait(driver, 17);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(emplocationUrl);

FireFoxDriver

Make sure to align your path of the driver or even while using the WebDriver directly for both of the options FireFox or Chrome Drivers.

String driverPath = “src\\test\\resources\\framework\\testautomation\\commons\\utilities\\driver\\geckodriver.exe”;
System.setProperty(“webdriver.chrome.driver”, driverPath);
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions(“ — headless”);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);

WebSecurity Enable/Disable

Adding the security option to the driver option is also available for us to pick.

String driverPath = “src\\test\\resources\\framework\\testautomation\\commons\\utilities\\driver\\chromedriver.exe”;
//System.setProperty(“webdriver.chrome.driver”, “C:\\ProgramData\\Eclipse\\workspace\\src\\test\\java\\resources\\chromedriver.exe”);
System.setProperty(“webdriver.chrome.driver”, driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
chromeOptions.addArguments(“ — disable-web-security”);
chromeOptions.addArguments(“ — allow-running-insecure-content”);
chromeOptions.addArguments(“ — allow-insecure-localhost” );
chromeOptions.addArguments(“test-type”);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
chromeOptions.setHeadless(true);
ChromeDriver driver = new ChromeDriver(capabilities);
//WebDriver driver = new ChromeDriver();
//driver= new ChromeDriver(new ChromeDriverService.Builder().usingPort(34533).build());
WebDriverWait wait=new WebDriverWait(driver, 17);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//NO need to have the JWT below with the location url so it will not be redirected
driver.get(emplocationUrl);

--

--

Development & UX Researches
Development & UX Researches

Written by Development & UX Researches

A self-learner in the programming and UX industry. Keep yourself updated in this journey together ;)

No responses yet