All Projects → previousdeveloper → Selenium Best Practices

previousdeveloper / Selenium Best Practices

For writing maintainable and scalable E2E Test.

Programming Languages

java
68154 projects - #9 most used programming language

Labels

Projects that are alternatives of or similar to Selenium Best Practices

Tib
Easy e2e browser testing in Node
Stars: ✭ 64 (-33.33%)
Mutual labels:  selenium
Cfselenium
A native Selenium WebDriver binding for ColdFusion
Stars: ✭ 77 (-19.79%)
Mutual labels:  selenium
Glastoselenium
A bot for booking Glastonbury tickets using selenium
Stars: ✭ 89 (-7.29%)
Mutual labels:  selenium
Alipayspider Scrapy
AlipaySpider on Scrapy(use chrome driver); 支付宝爬虫(基于Scrapy)
Stars: ✭ 70 (-27.08%)
Mutual labels:  selenium
Xebium
Xebium provides Selenium (webdriver) bindings for FitNesse, with Selenium-IDE support
Stars: ✭ 73 (-23.96%)
Mutual labels:  selenium
Docker Python Xvfb Selenium Chrome Firefox
Dockerfiles for Python 3.6/2.7 & Selenium in a headless Chrome or Firefox environment
Stars: ✭ 82 (-14.58%)
Mutual labels:  selenium
Pyautotest
Stars: ✭ 61 (-36.46%)
Mutual labels:  selenium
Splashr
💦 Tools to Work with the 'Splash' JavaScript Rendering Service in R
Stars: ✭ 93 (-3.12%)
Mutual labels:  selenium
Mybatis Spring Boot Jpetstore
A sample web application built on MyBatis 3, Spring Boot and Thymeleaf 3.
Stars: ✭ 75 (-21.87%)
Mutual labels:  selenium
Instaloctrack
An Instagram OSINT tool to collect all the geotagged locations available on an Instagram profile in order to plot them on a map, and dump them in a JSON.
Stars: ✭ 85 (-11.46%)
Mutual labels:  selenium
Rules webtesting
Bazel rules to allow testing against a browser with WebDriver.
Stars: ✭ 70 (-27.08%)
Mutual labels:  selenium
Aspnetcoreactivedirectorystarterkit
Starter kit to quickly create ASP.NET Core with On-Premises Active Directory Authentication.
Stars: ✭ 71 (-26.04%)
Mutual labels:  selenium
Echo360
Commandline tool for automated downloads of echo360 videos hosted by university
Stars: ✭ 81 (-15.62%)
Mutual labels:  selenium
Supremedropbot
A supreme web bot, written in python, to grab a list of specified products, and checkout before they sell out!
Stars: ✭ 66 (-31.25%)
Mutual labels:  selenium
Webdriverextensions
Make your WebDriver based Selenium tests more readable, reusability and maintainable by using WebDriver Extensions!
Stars: ✭ 89 (-7.29%)
Mutual labels:  selenium
Integration tests
ManageIQ integration tests
Stars: ✭ 63 (-34.37%)
Mutual labels:  selenium
Spam Bot 3000
Social media research and promotion, semi-autonomous CLI bot
Stars: ✭ 79 (-17.71%)
Mutual labels:  selenium
Taffy
Test Automation Framework Based On Nosetests. ✨🍰✨
Stars: ✭ 94 (-2.08%)
Mutual labels:  selenium
Adidas Multi Session
(Python) Program to simulate multiple sessions on adidas queue pages.
Stars: ✭ 90 (-6.25%)
Mutual labels:  selenium
Protractor
E2E test framework for Angular apps
Stars: ✭ 8,792 (+9058.33%)
Mutual labels:  selenium

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(){
    //...
    }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].