Selenium Design Patterns - Strategy Design Pattern

Selenium Design Patterns - Strategy Design Pattern

In previous articles I was covering some of the useful design patterns used in Selenium. Today in this article I will be covering Strategy Design Pattern.

Its is about defining a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

That is, Strategy Pattern is useful when we have multiple algorithms for specific task and we want our application to be flexible to choose any of the algorithms at run time for specific task.

Google Search Strategy Interface -

import org.openqa.selenium.WebDriver;


public interface GoogleSearchStrategy {
    void setDriver(WebDriver driver);
    void search(String searchFor);
}

Google Search Strategy Implementation -

package utility;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;


public class GoogleSearchImplements implements GoogleSearchStrategy {
	


	    private WebDriver driver;
	    private By searchBox = By.name("q");
	    private By searchButton = By.name("btnK");


	    public void search(String searchFor) {
	        System.out.println("Searching using Text Straegy:" + searchFor);


	        driver.findElement(searchBox).sendKeys(searchFor);
	        driver.findElement(searchBox).sendKeys(Keys.ENTER);
	      //  driver.findElement(searchButton).click();
	    }


	    public void setDriver(WebDriver driver) {
	        this.driver = driver;
	    }


	}



Google Search Page Object Class -

package utility;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class GoogleSearchPage {
	


	    private WebDriver driver;
	    private GoogleSearchStrategy strategy;
	    private By result = By.className("rc");


	    public GoogleSearchPage(WebDriver driver, GoogleSearchStrategy stragey) {


	        this.driver = driver;
	        this.strategy = stragey;
	        this.strategy.setDriver(driver);


	        //navigate to google page
	        driver.get("https://www.google.lk");
	    }


	    public void search(String txt) {
	        this.strategy.search(txt);
	    }


	    public int getResultsCount() {
	        return this.driver.findElements(result).size();
	    }
	}




Test Class -

package utility;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class TestClass {


  
    WebDriver driver;


    @BeforeTest
    public void beforeTest() {
    	  System.setProperty("webdriver.chrome.driver","D:\\chrdrv\\chromedriver.exe");
		    driver = new ChromeDriver();
    }


 
    @AfterTest
    public void afterMethod() {
         driver.close();
    }




    @Test(dataProvider = "dp")
    public void googleSearchStrategy(GoogleSearchStrategy strategy, String searchString, int resultCount) {


        GoogleSearchPage google = new GoogleSearchPage(driver, strategy);
        google.search(searchString);
        Assert.assertEquals(resultCount, google.getResultsCount());


    }




    @DataProvider
    public Object[][] dp() {
        return new Object[][] {
            new Object[] {
                    new GoogleSearchImplements(), "Kushan Amarasiri", 8
                },
               
                new Object[] {
                    new GoogleSearchImplements(), "Selenium WebDriver", 14
                },
        };
    }
}



要查看或添加评论,请登录

Kushan Shalindra Amarasiri的更多文章

社区洞察

其他会员也浏览了