With the increase in the number of active internet users globally, websites have gained prominence for both customers and businesses. As user expectations are increasing, websites are getting more complex, layered, and function-rich than ever before. To engage and retain customers, developers are under mounting pressure to build better websites in shorter durations. These websites must have improved features, exceptional visuals, and technical excellence for customer delight.
Hence, website developers have to deliver better website features frequently to prevent the loss of traffic, revenue, and credibility. Test Automation is essential to meet the customers’ expectations in terms of quality and turnaround time. This is why browser automation is essential in the website development process, for testing the website thoroughly in a shorter span of time.
Table of ContentsBrowser Automation, as the name suggests, automates the testing of web applications in order to deliver a quality-rich product. Browser automation helps to interact with the browser’s web elements and perform actions as a real user would do. Selenium is one of the most popular browser automation frameworks that help to leverage the maximum benefits of browser automation.
Automation is the need of the hour with increasing demand for more and more complex web applications that are function rich, support advanced technologies, enhanced UI’s, etc. With these growing demands and restricted timelines, manual testing becomes difficult in order to achieve the expected quality and ROI (return on investment) through testing.
Test Automation can help to reduce manual efforts and time required for testing and also deliver a high-quality product depending upon the test automation strategy, tool, framework and resources used.
This way it helps to deliver a high quality product with less manual efforts in a short period of time. We usually automate regression test cases in order to avoid repetitive tasks. This helps to avoid any human errors if regression was to be done manually. Though 100% automation is not possible and manual efforts are always required, it reduces the possibility of human errors and also it is not a good idea to execute the regression suites manually during every release.
Moreover, apart from the functional regression tests, it is also important to ensure a consistent and seamless user experience across different browsers and devices. Which why Cross Browser Testing is essential to check if the website is compatible with different versions of browsers.
Cross Browser Testing is in trend as new browsers with different versions are being introduced in the market. It becomes necessary for developers to design applications that are compatible with all types of browser versions and devices. Browser automation can help to execute our tests across multiple device-browser combinations.
Pro Tip: Testing on a real device cloud like BrowserStack, helps you perform cross browser testing on 3000+ real browser-device combinations for a comprehensive testing experience.
Selenium WebDriver allows browser automation, by connecting client to server. There are driver classes for different browsers in Selenium, which implement the Webdriver interface in order to interact with the browser. Let’s understand what happens internally when you instantiate ChromeDriver.
WebDriver driver = new ChromeDriver();
Here we are initialising the ChromeDriver class that implements the WebDriver interface.
This ChromeDriver class will interact with the chrome browser to perform the user actions.
As seen in the above diagram, webdriver acts as an interface between client and server.
The below example shows how to launch different browsers like chrome, firefox, IE with the help of WebDriverManager class and properties file.
package com.qa.browserstack.base; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class BasePage { WebDriver driver; Properties prop; public WebDriver init_driver(Properties prop) { String browser = prop.getProperty("browser"); if(browser.equals("chrome")) { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); }else if(browser.equals("firefox")) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } else { System.out.println("Please provide a proper browser value.."); } driver.manage().window().fullscreen(); driver.manage().deleteAllCookies(); driver.get(prop.getProperty("url")); return driver; } public Properties init_properties() { prop = new Properties(); try { FileInputStream ip = new FileInputStream("Properties file path"); prop.load(ip); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return prop; } }
In this example, we are passing the browser value with the help of the properties file. Depending upon the given value, the specific driver will be instantiated. This is also a way, using which you can perform cross browser testing across different browsers.
On executing the above code for chrome browser, you can see the browser is launched successfully. Similarly you can launch other browsers as well.
While the above method explaining how to launch a browser, this method is used when you need to open an Incognito Window in Selenium.
ChromeOptions class in Selenium helps to open the browser in incognito mode.
You have to pass incognito as an argument to the addArguments method of ChromeOptions class.
Let’s take the same example as above and pass incognito as an argument while launching chrome browser
package com.qa.browserstack.base; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class BasePage { WebDriver driver; Properties prop; public WebDriver init_driver(Properties prop) { String browser = prop.getProperty("browser"); if(browser.equals("chrome")) { ChromeOptions option = new ChromeOptions(); option.addArguments("incognito"); WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(option); }else if(browser.equals("firefox")) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } else { System.out.println("Please provide a proper browser value.."); } driver.manage().window().fullscreen(); driver.manage().deleteAllCookies(); driver.get(prop.getProperty("url")); return driver; } public Properties init_properties() { prop = new Properties(); try { FileInputStream ip = new FileInputStream("Properties File Path"); prop.load(ip); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return prop; } }
Chrome browser opens in incognito mode as seen below.
To open a page or website in Selenium, you need to pass the url of the page in driver.get() method. In this example, we are passing the url through the properties file. Then using the driver.get() method to access the specified url.
package com.qa.browserstack.base; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class BasePage { WebDriver driver; Properties prop; public WebDriver init_driver(Properties prop) { String browser = prop.getProperty("browser"); if(browser.equals("chrome")) { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); }else if(browser.equals("firefox")) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } else { System.out.println("Please provide a proper browser value.."); } driver.get(prop.getProperty("url")); return driver; } public Properties init_properties() { prop = new Properties(); try { FileInputStream ip = new FileInputStream("Properties File Path"); prop.load(ip); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return prop; } }
The above code opens browserstack.com URL in Chrome browser as seen in the image below.
Google Search is widely used by the end-users, and it makes a significant use case for Browser Automation. For any website, it is important that users are able to search, find the website in Google SERP (Search Engine Results Page), and click to access the website. This makes it a valid test case for Selenium Browser Automation.
Let’s understand Google Search Automation in Selenium using the example of searching “browserstack” string in Google.
Whenever you enter a search string in the google search box, it displays multiple options in suggestions. These suggestions are displayed with the help of Ajax (Asynchronous javascript and XML). It is a technology used for creating interactive web applications.
You can store these suggestions and select the suitable option with the help of Selenium.
import java.time.Duration; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class GoogleSearchTest { public static void main(String[] args) { String Searchstring = "browserstack"; WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); String url = "https://www.google.com/"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20)); driver.findElement(By.name("q")).sendKeys(Searchstring); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20)); List<WebElement> searchitems = driver.findElements(By.xpath("//ul[@role='listbox']/li/descendant::div[@class='eIPGRd']")); for(int i=0;i<searchitems.size();i++) { String listitem = searchitems.get(i).getText(); System.out.println(listitem); if(listitem.contains(Searchstring)) { searchitems.get(i).click(); break; } } } }
The above code searches browserstack string in the Google Search Box, as seen in the image below.
In the above code, the list of all suggestions displayed in google search is captured using Selenium and then select the required search string.
Console Output:
BrowserStack Automate allows us to execute tests on various device-browser combinations which is helpful when the test suites are large and you need to perform parallel testing to run test cases simultaneously on different device-browser combinations.
Launch Browser on BrowserStack Cloud Selenium Grid
Let’s see an example to launch browser on BrowserStack:
Signup for Free, and get your BrowserStack username and access key before executing the below code.You can get your username and access key once you login to BrowserStack, Navigate to Dashboard and Click on Access Key as shown below.
package BS; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URL; public class LaunchBrowserOnBS { public static final String AUTOMATE_USERNAME = "your_username"; public static final String AUTOMATE_ACCESS_KEY = "your_password"; public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; public static void main(String[] args) throws Exception { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("os_version", "10"); caps.setCapability("resolution", "1920x1080"); caps.setCapability("browser", "Chrome"); caps.setCapability("browser_version", "latest"); caps.setCapability("os", "Windows"); caps.setCapability("name", "Test to launch chrome browser on BS"); // test name final WebDriver driver = new RemoteWebDriver(new URL(URL), caps); try { // go to bstackdemo.com driver.get("https://www.browserstack.com/"); } catch (Exception e) { // print any exception System.out.println(e); } // quit the driver driver.quit(); } }
Here’s how the BrowserStack Automate Dashboard looks like with all the details like Text Logs, Network Logs, and Video Logs.
BrowserStack Automate Dashboard also contains the details of Input Capabilites and Device Capabilities as seen below.
You can play the test that has been performed or even download the test case. BrowserStack provides a wide range of capabilities that detail every step in detail along with the time of execution. Using Selenium IDE, you can record and playback the Browser Automation test for better debugging failed tests.
Try Browser Automation on Cloud Selenium Grid
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6lmK6mk516o77OsKqeql2eu26%2FxKWcp6Glog%3D%3D