Can Selenium IDE/Builder run same test case on many pages? Can Selenium IDE/Builder run same test case on many pages? jenkins jenkins

Can Selenium IDE/Builder run same test case on many pages?


The simple answer is "no" but Selenium WebDriver is one of the best choices for sure to find the links of a page and iterate over them. There is a very similar concept of your UIMapping called PageFactory where you map all the page elements in separate classes to keep the responsibility separate which makes debugging and refactoring much easier. I have used the PageFactory concept here.

Now coming back to your question, you can easily find the list of the links present in a page. In that case you just need to write the selector little carefully. You can then easily iterate over the links and come back and forth and so on.

Proof of concept on Google

BasePage

package google;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.support.PageFactory;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;import java.util.NoSuchElementException;/** * Defines the generic methods/functions for PageObjects. */public class BaseClass {    protected WebDriver driver;    /**     * @param _driver     * @param byKnownElement     */    public BaseClass(WebDriver _driver, By byKnownElement) {        //assigning driver instance globally.        driver = _driver;        this.correctPageLoadedCheck(byKnownElement);        /* Instantiating all elements since this is super class        and inherited by each and every page object */        PageFactory.initElements(driver, this);    }    /**     * Verifies correct page was returned.     *     * @param by     */    private void correctPageLoadedCheck(By by) {        try {            driver.findElement(by).isDisplayed();        } catch (NoSuchElementException ex) {            throw ex;        }    }}

PageObject inherited BasePage

package google;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.support.FindBy;import org.openqa.selenium.support.How;import java.util.List;/** * Created by Saifur on 5/30/2015. */public class GoogleLandingPage extends BaseClass {    private static final By byKnownElement = By.xpath("//a[text()='Sign in']");    /**     * @param _driver     */    public GoogleLandingPage(WebDriver _driver) {        super(_driver, byKnownElement);    }    //This should find all the links of the page    //You need to write the selector such a way    // so that it will grab all intended links.    @FindBy(how = How.CSS,using = ".gb_e.gb_0c.gb_r.gb_Zc.gb_3c.gb_oa>div:first-child a")    public List<WebElement> ListOfLinks;}

BaseTest

package tests;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;public class BaseTest {    public WebDriver driver;    String url = "https://www.google.com/";    @BeforeClass    public void SetUpTests() {        driver = new FirefoxDriver();        //Navigate to url        driver.navigate().to(url);        //Maximize the browser window        driver.manage().window().maximize();    }    @AfterClass    public void CleanUpDriver() throws Exception {        try {            driver.quit();        }catch (Exception ex){            throw ex;        }    }} 

Link Iterator test inheriting BaseTest

package tests;import google.GoogleLandingPage;import org.openqa.selenium.WebElement;import org.testng.annotations.Test;import java.util.List;/** * Created by Saifur on 5/30/2015. */public class LinksIteratorTests extends BaseTest {    @Test    public void IterateOverLinks(){        GoogleLandingPage google = new GoogleLandingPage(driver);        List<WebElement> elementList = google.ListOfLinks;        for (int i=0;i<elementList.size(); i++){            elementList.get(i).click();            //possibly do something else to go back to the previous page            driver.navigate().back();        }    }}

Note: I am using TestNG to maintain the tests and please note for lazy loading page you may need to add some explicit wait if necessary


Actually it's simple to run an IDE test against 1 specific page (base url actually): java -jar selenium-server.jar -htmlSuite "*firefox" "http://baseURL.com" "mytestsuite.html" "results.html"

So what you need to do is use jenkins (or any bash/batch script) to run that command multiple times with the base url set as "http://baseURL.com/page1", "http://baseURL.com/page2", etc.

This will only get you as far as static list of pages to test against. If you want a dynamic list you'd have to also "crawl" the pages and you could do that in the similar batch/bash script to obtain the list of pages to test against.

In this case you'd best be investing beyond selenium IDE and switch to webdriver where you'll have more power to loop and flow control.