• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

For writing maintainable and scalable E2E Test.

Goal

Our goal is to get together Selenium best practices. Feedback is welcome. If you see anything wrong or know better way open an issue or create a pull request, just fork it and change it. We are open to learn and hear new ideas.

Selenium-best-practices

Selenium is a portable software testing framework for web applications. It also provides a test domain-specific language to write tests in a number of popular programming languages, including Java, C#, Groovy, Perl, PHP, Python and Ruby.

Use PageObjects Pattern

Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. An implementation of the page object model can be achieved by separating the abstraction of the test object and the test scripts.

Advantages of using Page Object Pattern:

  • Easy to Maintain
  • Easy Readability of scripts
  • Reduce or Eliminate duplicacy
  • Re-usability of code
  • Reliability
  • There is a clean separation between test code and page specific code such as locators and layout.

Use multi config for each environment. Make it changeable. Read your test case value from config. Don't use a static.

Shortcut

  • Don't use magic strings.
  • Separate to WebPageElement, WebPageObject, WebTest.

Prefered Selector Order

Preferred selector order : id > name >links text> css > xpath

CSS and XPath are location based selectors, they are slower than other selectors.

  • Id and name are often the easiest and sure way.
  • CSS = Id + name.
  • Last solution should be xpath.

Create Ordered Tests

import org.junit.runners.MethodSorters;

//Running tests in order of method names in ascending order
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
//Running tests in order of method names in ascending order
@FixMethodOrder(MethodSorters.JVM)

Get Name of the Running Test

import org.junit.rules.TestName;

@Rule
public TestName name = new TestName();

//example usage; writes every test name before it runs.
@Before
    public void logBeforeTestsStart() {
        log.info(name.getMethodName());
    }

Do not use Magic Strings

It improves readability of the code and it's easier to maintain.

WebDriver driver = new HtmlUnitDriver();
// Don't use, create Constants each element name
WebElement element = driver.findElement(By.name("sample"));
driver.quit();

Behavior Driven Design

BDD is principally an idea about how software development should be managed by both business interests and technical insight, the practice of BDD assumes the use of specialized software tools to support the development process. Think all test scenarios.

Example BDD

Feature: Sign up

Sign up should be quick.
Write all steps.
1- Open Browser
2-Navigate to Url
3-Click Sign Up Button
4-Write Valid Username and Password
5-Click Register.

Scenario: Successful sign up
New users should get a confirmation email and be greeted
personally by the site once signed in.

Given I have chosen to sign up
When I sign up with valid details
Then I should receive a confirmation email
And I should see a personalized greeting message

Scenario: Duplicate email

Where someone tries to create an account for an email address
that already exists.

Given I have chosen to sign up
But I enter an email address that has already registered
Then I should be told that the email is already registered
And I should be offered the option to recover my password

###Simple Example

##Page Element Class

//Write clear class  name.
public class WebLoginPageElement {

    private String emailInputId;
    private String passwordInputId;
    public WebLoginPageElement() {
        this.emailInputId = "Email";
        this.passwordInputId = "Password";
        }
        
    public String getEmailInputId() {
        return emailInputId;}
    
    public String getPasswordInputId() {
        return passwordInputId;}
        

##Page Object Class

public class WebLoginPage extends Function {

   //Make own custom WebElement Class.
   private WebElementFactory elementFactory;
   private IWebAction elementAction;
   private WebLoginPageElement loginPageElement;

   public WebLoginPage() {
     elementFactory = new WebElementFactory(driver);
     elementAction = new WebAction(driver);
     loginPageElement = new WebLoginPageElement();
    }


   public WebLoginPage login(String email, String password) {
    
    WebElement emailInputElement = 
      elementFactory.createElementById(loginPageElement.getEmailInputId());
      elementAction.clear(emailInputElement);
      elementAction.sendKeys(emailInputElement, email);

    WebElement passwordInputElement = 
      elementFactory.createElementById(loginPageElement.getPasswordInputId());
      elementAction.clear(passwordInputElement);
      elementAction.sendKeys(passwordInputElement, password);

    WebElement loginSubmitButton =
      elementFactory.createElementByCssSelector(loginPageElement.getSubmitBtnCss());
      elementAction.click(loginSubmitButton);
        return this;}

##Test Class

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WebLoginTest extends Function {

    private static org.apache.log4j.Logger log = Logger.getLogger(WebLoginTest.class);
    WebElementFactory elementFactory = new WebElementFactory(driver);
    private static WebLoginPageElement loginPageElement = new WebLoginPageElement();
    
    @Rule
    public TestName name = new TestName();

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        PropertyConfigurator.configure(IConstants.LOG4j_PROP);
        openBrowser(RunAllWebTests.getConfigProperties().getLoginUrl());
    }
       @AfterClass
    public static void tearDownAfterClass() throws Exception {
        driver.close();
        driver.quit();
    }
    
    //Test all scenarios. 
    @Test
    public void loginWithEmptyUsername() throws Exception {
        //Call loginPage 
        new WebLoginPage().login(
                RunAllWebTests.getConfigProperties().getEmptyUsername(),
                RunAllWebTests.getConfigProperties().getValidPassword())
                .assertTruePageContain(loginPageElement.getEmptyUsernameAssert());
    }
    
    @Test
    public void loginWithWrongFormat(){
    //...
    }
    
    @Test
    public void loginWithWrongFormatId(){
    //...
    }

More Repositories

1

angular2-simple-countdown

a simple countdown angular2 directive with multiple language
TypeScript
27
star
2

angular2-blog

This is a angular2-sample that shows blog post with rest api.
JavaScript
14
star
3

translater

Translate is a command-line interface to the MyMemory translated
JavaScript
12
star
4

Retrofit.OAuth-2

Simple OAuth2 Resource Owner Password Credentials Example
Java
11
star
5

angular-regex

Angular regular expressions service
JavaScript
10
star
6

ionic-best-structure

Clean code for ionic project.
JavaScript
10
star
7

spark-jwt-auth

JSON Web Token Authentication with SparkFramework
Java
10
star
8

java-distributed-data-cache

Spring Boot, Quarkus Distributed Cache project implements (property-based) configuring of multiple backend providers
9
star
9

staj.io

Türkiyede stajyer alan firmaların bilgileri listeleyen uygulamadır.
CSS
9
star
10

angular-simple-countdown

a simple countdown angular directive with multiple language.
JavaScript
8
star
11

PythonPostcodesWrapper

Simple wrapper around postcodes.io
Python
8
star
12

als-collector

Envoy access log collector to multiple datasource
Go
7
star
13

Evender

Event Finder Restful Service In Turkey
Java
7
star
14

angular-turkiye-haritasi

Angular SVG Türkiye Haritası
HTML
5
star
15

Jakbank

Java client library for Akbank Api
Java
4
star
16

stash-deep-clone

deep clone for stash projects
Python
4
star
17

Movie-Recommendation

Integration RoboGuice, Retrofit,Jsoup Parsing.
Java
4
star
18

angular2-camelcase

Angular2 pipe to convert camelCase strings to human readable strings
TypeScript
3
star
19

AspNetCore-Angular2-Example

Asp.Net Core Angular 2 single page web application Seed
HTML
2
star
20

lister-checker

Lister is a image checker for given text file.
JavaScript
2
star
21

chevent-android

android-app
Java
2
star
22

heatr

Turkish software company location based heat map
JavaScript
2
star
23

ConsulKvClient

Consul KV library for c#
C#
2
star
24

Social-Media-Contact-Finder

Social Media Contact Finder is android application which uses fullcontact backend api.
Java
2
star
25

rospock-scala

Rock, Paper, Scissors game implementation with additional weapons support.
Scala
2
star
26

istio-simple-concepts

Simple istio concepts
2
star
27

angular-microfaker

It easier to create tests data objects.
JavaScript
2
star
28

fil

Simple File Decorator
JavaScript
2
star
29

flagger-dashboard

CSS
1
star
30

proxy-assemblyscript-envoy

TypeScript
1
star
31

ionic-hackathon-starter

A boilerplate for ionic applications
JavaScript
1
star
32

remote-jit-sample

Java
1
star
33

es-spark

Simple Elasticsearch Spark Example
Scala
1
star
34

postcodes-go-client

Go library for interacting with the excellent Postcodes.io service
Go
1
star
35

BusTimeOfBalikesir

Java
1
star
36

confuq

Confuq is simple configuration library
C#
1
star
37

angular2-playground

for fun.
TypeScript
1
star
38

couchbase-load-test-pillowfight

couchbase load test tool
Dockerfile
1
star