Viewer Counter: 189
In 2023, Selenium is still a popular automation testing tool. It does support many running mode to adapt to your requirements.
Headless Mode is a feature that allows the browser to run without a graphical user interface (GUI), making it faster and more efficient for running automated tests. It is recommend when running testcases in CI/CD where you can not see the browser.
Otherwise, Private Mode refers to running a browser session in incognito/private mode.
When using this mode, the browser does not store any browsing history, cookies, or form data. This is extremely useful as your testcases
I will go straight forward to how setup headless and private mode. If you do not know how to init a Selenium project in Java, C# or Python. Please read this blog first.
Also note that the recent Selenium version using in this blog is 4.8.1 (latest until 3/9/2023). Maybe it will be outdated in the future.
You can set up headless and private mode as below:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
def program():
ser = Service(r'./chromedriver.exe')
op = webdriver.ChromeOptions()
op.add_argument('--headless')
op.add_argument('--incognito')
driver = webdriver.Chrome(service=ser, options=op)
driver.get('https://tomatoqa.com')
assert driver.title == 'Home - tomatoqa.com'
driver.quit()
if __name__ == '__main__':
program()
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
public class HeadlessChrome {
public static void main(String[] args) throws InterruptedException {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectPath + "/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--incognito");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://tomatoqa.com");
Assert.assertEquals(driver.getTitle(), "Home - tomatoqa.com");
driver.quit();
}
}
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
ChromeOptions chromeOptions = new();
chromeOptions.AddArgument("—headless");
chromeOptions.AddArgument("--incognito");
IWebDriver driver = new ChromeDriver(chromeOptions);
driver.Url = "https://tomatoqa.com";
Assert.AreEqual(driver.Title, "Home - tomatoqa.coms");
driver.Quit();
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
def program():
service = Service(r'./geckodriver.exe')
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--private')
options.binary_location = FirefoxBinary()
driver = webdriver.Firefox(service=service, options=options)
driver.get('https://tomatoqa.com')
driver.maximize_window()
assert driver.title == 'Home - tomatoqa.com'
driver.quit()
if __name__ == '__main__':
program()
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.testng.Assert;
public class HeadlessChrome {
public static void main(String[] args) throws InterruptedException {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.gecko.driver", projectPath + "/geckodriver.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary();
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--headless");
firefoxOptions.addArguments("--private");
firefoxOptions.setBinary(firefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("https://tomatoqa.com");
Assert.assertEquals(driver.getTitle(), "Home - tomatoqa.com");
driver.quit();
}
}
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
FirefoxOptions firefoxOptions = new();
firefoxOptions.AddArgument("--headless");
firefoxOptions.AddArgument("--private");
IWebDriver driver = new FirefoxDriver(firefoxOptions);
driver.Url = "https://tomatoqa.com";
Assert.AreEqual(driver.Title, "Home - tomatoqa.com");
driver.Quit();
from selenium import webdriver
from selenium.webdriver.edge.service import Service
def program():
service = Service(r'./msedgedriver.exe')
options = webdriver.EdgeOptions()
options.add_argument('--headless')
options.add_argument('InPrivate')
driver = webdriver.Edge(service=service, options=options)
driver.get('https://tomatoqa.com')
driver.maximize_window()
assert driver.title == 'Home - tomatoqa.com'
driver.quit()
if __name__ == '__main__':
program()
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.testng.Assert;
public class HeadlessChrome {
public static void main(String[] args) throws InterruptedException {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.edge.driver", projectPath + "/msedgedriver.exe");
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("--headless");
edgeOptions.addArguments("InPrivate");
WebDriver driver = new EdgeDriver(edgeOptions);
driver.get("https://tomatoqa.com");
Assert.assertEquals(driver.getTitle(), "Home - tomatoqa.com");
driver.quit();
}
}
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
EdgeOptions edgeOptions = new();
edgeOptions.AddArgument("--headless");
edgeOptions.AddArgument("InPrivate");
IWebDriver driver = new EdgeDriver(edgeOptions);
driver.Url = "https://tomatoqa.com";
Assert.AreEqual(driver.Title, "Home - tomatoqa.coms");
driver.Quit();
Whenever you have a plan to run your automated testcases in CI/CD, consider running it in headless and private mode as it will make your testcases run more stable and a little bit faster.
I used to be a back-end Java and PHP developer but now I found joy in automation testing engineer position and sharing knowledge about it
Related