Selenium Css Property Validation Using Excel

Test Data

  • Create Excel workbook
  • 1st Col = ElementName
  • 2nd Col = ElementSelector
  • 2nd Col = ElementSelectorString
  • Set column headers for each CssProperty

 

Example:

ElementName ElementXPath A-B accelerator azimuth background background-attachment background-color background-image
WashingtonWatchSectionTitle //h3[contains(.,"Washington Watch") and @class="section-title"]/..           rgba(68, 108, 169, 1)  

 

TestBaseUtil: TestNG DataProvider

( or Class that reads Excel to HashMap<String,String>

  • Create a data provider that reads from Excel
    // DataProvider with HashTable
    @DataProvider(name="getCssValuesData")
    public static Object[][] getCssValuesData(Method m){
        
        String sheetName = m.getName();
        
        int rows = excel.getRowCount(sheetName);
        //int rows = 2;
        
        int cols = excel.getColumnCount(sheetName);
        //int cols = 4;
        
        System.out.println("worksheet rows/cols" + rows +"+"+cols);
        
        
        //set returned object for multiple rows & 1 column
        Object[][] data = new Object[rows-1][1];
        
        Hashtable<String,String> table = null;        
        for (int rowNum = 2; rowNum <= rows; rowNum++) {
            table = new Hashtable<String,String>();
            for (int colNum=0;colNum < cols; colNum++) {
                
                
                if(!excel.getCellData(sheetName, colNum, rowNum).isEmpty()) {
                    
                
                
                    //data[0][0]
                    //get headers from ROW1, each column
                    table.put(excel.getCellData(sheetName, colNum, 1), excel.getCellData(sheetName, colNum, rowNum));
                    
                    //data[table][0] = 'single column array with each row 'cell' containing the HashTable [with Header,Value pairs]
                    data[rowNum-2][0] = table;
                    
                }
                
                
            }
        }
        return data;
    }

 

 

TestBase

  • While this class is beyond this particular topic...
  • WebDriver instantiation, ExtentTest and TestNG setup exist here
  • logInfo() adds a step to extent reports
  • verifyStringEquals() is a wrapper for TestNG assertEquals, that has ExtentReport logging and anything else that facilitates test/reporting/logging

 

 

Test Class

  • Since the data object has 2 columns that are for Element identification, these two "keys" should be filtered when iterating through the HashMap
  • Additionally, if any field is EMPTY, don't use it to for validation.
  • This allows for flexible data sets to be created that won't cause false negative due to null value to validate against
   
    @Test(dataProviderClass = TestUtilPom.class, dataProvider = "getCssValuesData")
    public void CssValues(Hashtable<String, String> data) throws InterruptedException {
        System.out.println("TestCaseRunning.....");

        String filespec = "/src/test/resources/excel/CssValues.xlsx";
        loadExcelTestData(filespec);

        ElementPage ep = new ElementPage(driver);
        ep.gotoPage("https://www.schwab.com/resource-center/insights/");

        String xpath = data.get("ElementXPath");
        logInfo("Validating Element: " + xpath);
        el = ep.getWebElementDD(xpath);

        for (String cssprop : data.keySet()) {

            if (!cssprop.equals("ElementXPath") && !cssprop.equals("ElementName")) {
                System.out.println(cssprop);
                logInfo("Validating CssPropery: " + cssprop);
                verifyStringEquals(el.getCssValue(cssprop), data.get(cssprop));
            }
        }
    }

 

Console Output

[RemoteTestNG] detected TestNG version 6.14.2
log4j:WARN No appenders could be found for logger (devpinoyLogger).
Linuxlog4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Starting ChromeDriver 2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5) on port 3555
Only local connections are allowed.
Using PPAPI flash.
May 19, 2019 10:04:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Chrome: /home/silosix/git/cmsauto/src/test/resources/executables/chromedriver-2.45-linux64/chromedriver
worksheet rows/cols2+200
TestCaseRunning.....
background-color

===============================================
Sandbox Tests
Total tests run: 1, Failures: 0, Skips: 0

 

ReportNG

CSSVALUES
 
2019-05-19 21:29:15 2019-05-19 21:29:19 0h 0m 3s+630ms
 
 
Status Timestamp Details
  21:29:15 CSSVALUES: START
  21:29:19 Getting Page
  21:29:19 Validating Element: //h3[contains(.,"Washington Watch") and @class="section-title"]/..
  21:29:19 Validating CssPropery: background-color
  21:29:19 Asserting String Equals: ACTUAL: rgba(68, 108, 169, 1) EXPECTED: rgba(68, 108, 169, 1)
  21:29:19 CSSVALUES: DONE

 

 

 

 

 

 

 

 

Tags