1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package ChromeHeadless; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; /** * * @author Sarang Holey * * 10:49:00 pm */ public class BrowserHeadlessImplimentations { // creating WebDriver instance as static static WebDriver driver; public static void main(String[] args) { // calling static chrome driver headless method ChromeHeadless("https://www.facebook.com"); // calling static firefox driver headless method FirefoxHeadless("https://www.flipkart.com"); } /** * * @param URL */ public static void ChromeHeadless(String URL) { // setting up chromedriver system properties System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); //creating ChromeOptions object ChromeOptions options = new ChromeOptions(); // adding arguments for activation of chrome browser in headless mode options.addArguments("--headless"); // creating ChromeDriver instance with argument to the constructor ChromeOptions instance driver = new ChromeDriver(options); // passing the URL from method argument driver.get(URL); // printing title of the given URL page of the Website System.out.println(driver.getTitle()); } /** * * @param URL */ public static void FirefoxHeadless(String URL) { // creating object of firefox binary FirefoxBinary firefoxbinary = new FirefoxBinary(); // adding the argument to activate headless mode of firefox firefoxbinary.addCommandLineOptions("--headless"); // setting up chromedriver system properties System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); // creating FirefoxOptions object FirefoxOptions fo = new FirefoxOptions(); // setting up firefoxbinary fo.setBinary(firefoxbinary); // creating FirefoxDriver instance with argument to the constructor FirefoxOptions instance WebDriver driver = new FirefoxDriver(fo); // passing the URL from method argument driver.get(URL); // printing title of the given URL page of the Website System.out.println(driver.getTitle()); } } |