How to run Selenium Headless Browser in Ubuntu ?

Hello again !!!

Today I will post a tutorial about how configure and run selenium headless in Ubuntu using Mozilla Firefox as your primary browser.

Install Firefox headless in Ubuntu

Download the latest version of Firefox on your PC. In case you don’t have firefox or using older version of firefox, follow the steps to upgrade firefox.

  • In /etc/apt/sources.list add the following line.
	ppa:mozillateam/firefox-stable
  • Run the following commands to upgrade or install latest version of Firefox to work with Selenium.
	sudo apt-get update
	sudo apt-get install firefox

Install Xvfb – the X Virtual FrameBuffer

This piece of software emulates the framebuffer using virtual memory which lets you run X-Server in machines with no display devices. This service is required to make browsers run normally by making them believe there is a display available. To install xvfb in ubuntu or Debian, run the following command:

sudo apt-get install xvfb

Now run the xvfb service in a display number which is less likely to clash even if you add a display at later stage. For this example, you can assume a display, 10.

sudo Xvfb :10 -ac

The parameter -ac makes xvfb run with access control off. The server should be running now.

Start browser headlessly in Ubuntu

Before you can run a browser, you need to set the environment variable DISPLAY with the display number at which xvfb is running. Before we add the environmental variable as a persistent one, let us check whether everything works as expected.

export DISPLAY=:10
firefox

If there was no error on the terminal, then you have successfully running firefox headlessly in Ubuntu. The command should keep running until you kill the process by pressing ctrl+ C or similar. There wont be any output.

Now that you have a browser running headless in Ubuntu server, you can run selenium server as you will run in your local machine.

Easy, don’t you think ? If you have any questions, just write below !

Thank you ! Have a good weekend !

 

Fonts: http://www.installationpage.com/selenium/how-to-run-selenium-headless-firefox-in-ubuntu/

Using DOM on Selenium

Hello guys, today I will post about DOM, what is and where you will use on Automation.

What is the Document Object Model?

The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page. 

Why the Document Object Model?

“Dynamic HTML” is a term used by some vendors to describe the combination of HTML, style sheets and scripts that allows documents to be animated.
DOM lookups in a view
Let’s look at some example code. The first thing you can do is to create a container class called LoginContainer. This class is used to find the DOM elements of the Login View. For the DOM lookup, you can use the @FindBy annotation, which takes parameters specifying what to find. In Listing 10, we use the How parameter for the lookup and using as the lookup variable. How options include CSS, ID, and XPATH.

Listing 10. LoginContainer with DOM lookups on Ruby, Cucumber and Selenium

	@FindBy(how = How.ID, using = "LoginPage")
	public WebElement loginPageDiv;
	
	@FindBy(how = How.CSS, using = "#LoginPage input[name=username]")
	public WebElement usernameInput;
	
	@FindBy(how = How.CSS, using = "#LoginPage input[name=password]")
	public WebElement passwordInput;
	
	@FindBy(how = How.CSS, using = "#LoginPage span[role='button']")
	public WebElement submitButton;

A more generically class on Java and Selenium to Find a Element

public void FindElement(Parameters_Config configParam, String idName, 
String findBy) throws IOException, Exception {
        switch (findBy) {

            case "linkText":
                try {
                    configParam.driver.findElement(By.linkText(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            case "className":
                try {
                    configParam.driver.findElement(By.className(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            case "id":
                try {
                    configParam.driver.findElement(By.id(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            case "cssSelector":
                try {
                    configParam.driver.findElement(By.cssSelector(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            case "tagName":
                try {
                    configParam.driver.findElement(By.tagName(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            case "xpath":
                try {
                    configParam.driver.findElement(By.xpath(idName));
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;

            default:
                Exception e = new Exception();
                System.out.println(e.getMessage());
                break;
        }
    } 

Thank you 🙂

Font: http://www.w3.org/DOM/