Wednesday, June 26, 2024

Handling Web table in Selenium

 

Webtable:

 

Develop table using html :

1. Develop Simple Test table using html :

ex:

 

<html>

            Sample Tables:

   1. Simple table with rows and columns

 

<html>

 

<table border="1">

            <tr> 

            <th> TCID </th>

               <th> Test Case Description   </th>

                           <th> Expected Value    </th>

                        <th> Actual Value </th>

                        <th> Result</th>

            </tr>

 

            <tr>

              <td> TC_01    </td>

              <td> 1.Verify login    </td>

              <td> Login must be successfull         </td>

              <td> Login is successfull       </td>

            <td>Pass         </td>  

 

 

            </tr>

           

            <tr>

                        <td>TC_02     </td>

                        <td> Verify Logout     </td>

                        <td> Logout must be successfull    </td>

                        <td>                               Logout is successfull   </td>

                        <td>                               Fail   </td>

 

 

            </tr>                           

                       

            <tr>    

                        <td>  TC_03                </td>

                        <td>  Verify Order creation                </td>

                        <td>  Order must be created  </td>

                        <td>                            Order is created         </td>

                        <td>    Pass       </td>

 

            </tr>                                                                                       

                         

 

 

</table>

 

 

2. Develop  Table With Links and checkboxes:

 

<table border =2>

    <tr>

            <th>Activity Code       </th>

            <th>    Activity description  </th>

            <th> Branch     </th>

            <th>channel   </th>

            <th>Terminate Activity                                                                                   </th>

 

    </tr>

 

        <tr>

                <td> <a href="https://www.google.com/"> 101101             </a>  </td>

<td>  Rs.10 per unit off on Surf XL Blue Powder 3.3 kg in aLL india MOC      </td>

<td>    CHENNAI  </td>

<td>    GT </td>

<td>  <input type="checkbox">   <input type="checkbox">  </td>

                                               

            </tr>

           

                                     

 

        <tr>

                <td> <a href="https://www.facebook.com/"> 101102        </a>  </td>

<td>  Rs.10 per unit off on Surf XL Blue Powder 3.3 kg in aLL india MOC                  </td>

<td>    Bangalore</td>

<td>    GT </td>

<td>  <input type="checkbox">   <input type="checkbox">  </td>

                                               

            </tr>

           

</table>                       

                                      

 

 

 

</html>  

                                   

           

-----------------------

// HW Develop a customer table   with 4 rows of data

 Sno     Customer Name         CustomerID  City

 

 

 

//HW Develop Employee table

Ename Empid  City (Dropdown)         Delete (Checkbox)

ram    101        BNG,CHN,HYD        1 checkbox

sam    102        BNG,CHN,HYD        1 checkbox

raj    102          BNG,CHN,HYD        1 checkbox

 

Revise:

 

  <table>

            <th>  -- table heading

      <tr >     --> table row

         <td >   tc01  </td>  -- > table data or Column -1

         <td >   description </td>  -   colum2

            <td >  <input type="checkbox"  </td>

            <td >  <input type="text"  </td>

 

            </tr>   

 

 

 

Navigate to Table  using Xpath:

 

Navigate to all 'tr' tags from 'test case' table:

 

xpath -->   //table[@id='TestCaseTable01']/tbody/tr         - 4 ele found

 

Navigate to 1st 'tr' tag from 'test case' table:

xpath -->  //table[@id='TestCaseTable01']/tbody/tr[1]

 

Navigate to 2nd 'tr' tag from 'test case' table:

xpath -->  //table[@id='TestCaseTable01']/tbody/tr[2]

 

Navigate to 5th tr tag from 'test case' table:

xpath-->   //table[@id='TestCaseTable01']/tbody/tr[5]       --  0 ele as 5th row  or 5th tr tag is not  available in table

 

XPath Advanced (or) XPath-Axis:

XPath axes are used to navigate to nodes relative to the current node in the XML document. Here, we'll go through some of the common axes and their syntax with examples.

 

1. child Axis:

  • Syntax: child::tagname
  • Description: Selects all children of the current node that match the tag name.

Q: navigate to child tag of 'tbody' from 'test case' table

//table[@id='TestCaseTable01']/tbody

 

xpath --> //table[@id='TestCaseTable01']/child::tbody

 

Q:navigate to  child tag of 'tbody' and child tag of 'tr' from test case table

xpath --> //table[@id='TestCaseTable01']/child::tbody/child::tr  4 ele found or 4 rows found

 

 

HW navigate to child tag of 'tbody' and child tag of 'tr' and child tags of 'th' from test case table

 

 

HW navigate to child tag of 'tbody' and child tag of 1st 'tr' and child tags of 'th' from test case table

HW navigate to child tag of 'tbody' and child tag of 2nd 'tr' and child tags of 'td' from test case table

2. parent Axis:

  • Syntax: parent::tagname
  • Description: Selects the parent of the current node.

 

navigate to tr[2] td[1] > parent of 'tr'  > parent of 'tbody'

xpath ->  //tr[2]/td[1]/parent::tr/parent::tbody   5 ele

 

navigate to tr[2] td[1] > parent of tr  > parent of tbody> parent table

xpath --> //tr[2]/td[1]/parent::tr/parent::tbody/parent::table[@id='TestCaseTable01']   1 ele

3. ancestor Axis:

  • Syntax: ancestor::tagname
  • Description: Selects all ancestors (parent, grandparent, etc.) of the current node

 

Represents parent, grandparents, grand grandparents of given tag

              

Navigate to  'TC Description' and its ancestor of type =  tr

xpath  -> //th[text()='Test Case Description']/ancestor::tr

 

Navigate to  'TC Description' and its ancestor of type =  tbody

xpath --> //th[text()='Test Case Description']/ancestor::tbody

 

Navigate to  'TC Description' and its ancestor of type =  table

xpath --> //th[text()='Test Case Description']/ancestor::table

 

4. descendant Axis:

  • Syntax: descendant::tagname
  • Description: Selects all descendants (children, grandchildren, etc.) of the current node.

Example:

Represents child , grandchildren, grand grandchildren of given tag...etc

 

navigate to 'test case' table  and its  child/grand child of type 'tbody'

xpath --> //table[@id='TestCaseTable01']/descendant::tbody

 

navigate to 'test case' table  and its  child/grand child of type 'tr'

xpath --> //table[@id='TestCaseTable01']/descendant::tr     4 ele found  - 4 rows

 

 

navigate to 'test case' table  and its  child/grand child of 'th'

xpath --> //table[@id='TestCaseTable01']/descendant::th   - 5 columns

 

HW navigate to 'test case' table  and its  child/grand child of 'td'

//table[@id='TestCaseTable01']/descendant::td

 

5. following-sibling Axis:

  • Syntax: following-sibling::tagname
  • Description: Selects all siblings after the current node

 

Sibling means if child tags have same parent --

   (or) 

If 2 tags are in the same line or level 

 

navigate to test case table and  tr[2] and its following-sibling of type 'tr'

xpath --> //table[@id='TestCaseTable01']/tbody/tr[2]/following-sibling::tr  2 ele

 

 

Navigate to test case table and tr[2] and its 1st - following-sibling of type 'tr'

xpath --> //table[@id='TestCaseTable01']/tbody/tr[2]/following-sibling::tr[1]

 

 

Navigate to test case table and tr[2] and its 2nd - following-sibling of type 'tr'

xpath --> //table[@id='TestCaseTable01']/tbody/tr[2]/following-sibling::tr[2]

 

 

HW navigate to test case table and tr[3] and its following-sibling of type 'tr'?

xpath -->

 

6. preceding-sibling Axis:

  • Syntax: preceding-sibling::tagname
  • Description: Selects all siblings before the current node.

Means previous sibling

 

Navigate to 'test case' table and  tr[2] and its preceding -sibling of type 'tr'

xpath --> //table[@id='TestCaseTable01']/tbody/tr[2]/preceding-sibling::tr

 

Navigate to test case table and  tr[2] and  1st preceding -sibling of type 'tr'

xpath -->  //table[@id='TestCaseTable01']/tbody/tr[2]/preceding-sibling::tr[1]

 

Navigate to test case table  and tr[3] and 2nd preceding -sibling of type 'tr'

xpath -->  //table[@id='TestCaseTable01']/tbody/tr[3]/preceding-sibling::tr

 

HW navigate to test case table and tr[3] and preceding -sibling of type 'tr'

xpath --> 

 

 

Identify checkbox where "Activity code" =101 ?

            1st identify ele  activity code =101

xpath -->   //table[@id='HulTablee01']/tbody/tr/td/a[text()='10110101']/parent::td/following-sibling::td[4]/input[1]

 

HW Identify checkbox where Activity code =102 ?

xpath -->

 

Identify branch name where activity code = 101 ?

xpath -->  //table[@id='HulTablee01']/tbody/tr/td/a[text()='10110101']/parent::td/following-sibling::td[2]

 

HW Identify branch name where activity code = 102 ?

xpath --> 

 

7. following Axis:

  • Syntax: following::tagname
  • Description: Selects everything in the document after the closing tag of the current node.

       From current node / element/ tag  :  it goes to next tag of given till end of html page

 

Navigate 'test case ' table  > tr[2] >  from tr[2] - any next  'tr' tags

xpath -->//table[@id='TestCaseTable01']/tbody/tr[2]/following::tr    -->  26 tr tags

 

8. preceding Axis:

  • Syntax: preceding::tagname
  • Description: Selects everything in the document before the starting tag of the current node.

Navigate 'Activity Code ' table  > tr[2] >  from tr[2] - any previous  'tr' tags

xpath --> //table[@id='HulTablee01']/tbody/tr[2]/preceding::tr

                                    from tr[2]  --       all previous 'tr' tags  till starting of html page

 

 

HW navigate 'Activity Code ' table  > tr[2] >  from tr[2] ->  1st previous  tr tag

xpath -->

 

HW navigate 'Activity Code ' table  > tr[2] >  from tr[2] ->  1st following tr tag

xpath -->

 

 

HW navigate 'Activity Code ' table  > tr[2] >  from tr[2] ->  2nd following tr tag

 

 

HW navigate 'Activity Code ' table  > tr[2] >  from tr[2] ->  3rd following tr tag

 

Rows count and Columns Count From Table:

 

package WebTableBasics;

 

import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class RowsColumnsCnt {

 

            public static void main(String[] args) {

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser // ctrl + shift + 0

                        WebDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

                       

                        //******************************************

                        //  get all rows cnt in 'test case' table

                        //table[@id='TestCaseTable01']/tbody/tr  - 4 ele  4 rows

                        //table[@id='TestCaseTable01']//tr - 4ele

                        List<WebElement> allTRTagsList = driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr"));

                        int rowsCnt =allTRTagsList.size();

                       

                        System.out.println("rowsCnt="+rowsCnt);// rowsCnt=4

 

                        // get columns cnt in table

                        //table[@id='TestCaseTable01']/tbody/tr[1]/th

                        //  (or) //table[@id='TestCaseTable01']//tr[1]/th

                        List<WebElement> allTHTagsList = driver.findElements(By.xpath("//table[@id='TestCaseTable01']//tr[1]/th"));

                        int colsCnt =  allTHTagsList.size();

 

                        System.out.println("colsCnt="+colsCnt);// colsCnt=5

 

 

                        // HW   Write xpath to get rows cnt without using tbody

 

                        // get rows cnt  Use child / descendant

                        //table[@id='TestCaseTable01']/child::tbody/child::tr

                        //table[@id='TestCaseTable01']/descendant::tr

 

                        // HW   Write xpath to get columns cnt without using tbody

                       

                        // HW   Write xpath to get columns cnt  using child/descendant

 

 

            }

 

}

 

 

// HW Get Rows Count from 'Activity Code' Table

// HW Get Columns Count from 'Activity Code' Table

 

Read data from table cell by cell :

 

package WebTableBasics;

 

import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class ReadDataFromTable {

 

            public static void main(String[] args) {

//                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        //*******************************

                        // read data from row -1  column -1             

                        //table[@id='TestCaseTable01']/tbody/tr[1]/th[1]

                        //table[@id='TestCaseTable01']//tr[1]/th[1]

                        String  data11 = driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[1]/th[1]")).getText();

                       

                        System.out.println("data11=" +data11);

                       

                        // HW read data from row -1  and Column -2

 

                        //HW read data from row -1  and Column -3

 

 

                        // get complete data from row-1

                        //                     TCID Test Case Description Expected Value Actual Value Result

                        String tr1Data = driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[1]")).getText();

                        ///   row1Data=TCID Test Case Description Expected Value Actual Value Result

                        System.out.println("row1Data="+tr1Data);

                        // row1Data=TCID Test Case Description Expected Value Actual Value Result

                       

                        // HW  get complete data from row-2

                       

//                     HW  get complete data from row-3

 

                        // get all column names //table[@id='TestCaseTable01']/tbody/tr[1]/th

                        List<WebElement> allTHTagsList = driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[1]/th"));

                        //                 <th>TCID</th>

                        //                 <th> Test Case Description  </th>

                       

                        for(int i=-0;i<=allTHTagsList.size()-1;i++)

                        {

                                    String columnNames  = allTHTagsList.get(i).getText();

                                    System.out.println("columnNames="+columnNames);

                        }

                                   

                       

                       

                        //                                     ColumnNames =TCID

                        //                                             ColumnNames =Test Case Description

                        //                                             ColumnNames =Expected Value

                        //                                             ColumnNames =Actual Value

                        //                                             ColumnNames =Result

 

 

                        // HW 2. get all columns   use "for each" loop

 

//                     / HW 3. get all columns  using iterator()

 

                        //HW 4.  get all columns  using listIterator()

 

 

            }

 

}

 

Read Data From Table :

 

package WebTableBasics;

 

import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class ReadDataCellWiseFromTable {

 

            public static void main(String[] args) {

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        //  get 2,1   2,2 ,23, cell 

                        //table[@id='TestCaseTable01']/tbody/tr[2]/td[1]  --> 2nd row and 1st column

                        //table[@id='TestCaseTable01']/tbody/tr[2]/td[2]  --> 2nd row and 2nd column

                        //table[@id='TestCaseTable01']/tbody/tr[2]/td[3]

                        //table[@id='TestCaseTable01']/tbody/tr[2]/td[4]

 

                        //table[@id='TestCaseTable01']/tbody/tr[3]/td[1]  --> 3rd row 1st Column

                        //table[@id='TestCaseTable01']/tbody/tr[3]/td[2]  --> 3rd row 2nd column

                        //table[@id='TestCaseTable01']/tbody/tr[3]/td[3]

 

 

                        //<td>TC_01</td>  -->   gettext()

                        String data21 =  driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td[1]")).getText();

                        //                    TC_01

                        System.out.println("data21="+ data21);//

 

 

                        //get data 2,2 cell

                        String data22 =  driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td[2]")).getText();

                        //                   

                        System.out.println("data22="+data22);// data22=Verify Login

 

                        // HW get  2nd row  3rd col data from test cases table

 

                        //                     HW get  2nd row  4th col data from test cases table

                        //                     HW get  2nd row  5th col data from test cases table

 

                        //                     HW get  2nd row  6th col data from test cases table(6th row is not there)

 

 

                        //                       get 2nd row  data from all columns 2,1 ,2,2, 2,3, 2,4 2,5

                        int colCnt =driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[1]/th")).size();

                        System.out.println("colCnt="+colCnt);

 

                        //                     driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td[1]")).getText();

                        //                     driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td[2]")).getText();

 

                        //                     driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td[5]")).getText();

 

 

                        //   2  + 3  =  5

                        //  "ram" +   3 =   ram3  - joining

                        for(int i=1;i<=colCnt-1;i++)

                        {

                                    String data =driver.findElement(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[2]/td["+i+  "]")).getText();

                                    //"//table[@id='TestCaseTable01']/tbody/tr[2]/td[1]

                                    System.out.println("get 2nd row data from all columns data ="+ data);

                        }

 

 

                        //HW   get 3rd row  data from all columns

 

                        //HW   get 4th row  data from all columns

 

 

                        // read all rows and all columns data

                        //   2,1  2,2  2,3   2,4  2,5

                        //   3,1  3,2  3,3   3,4  3,5

                        //

                        // get rows cnt

                        int rowsCnt =driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr")).size();

                        int colsCnt =driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr[1]/th")).size();

 

                        //                     for(int i=2;i<=4;i++)//  rows

                        for(int i=2;i<=rowsCnt;i++)//  rows

                        {

                                    //                                 for(int j=1;j<=5;j++)// columns j=1. 1+1 =2

                                    for(int j=1;j<=colsCnt;j++)// columns j=1. 1+1 =2

                                    {//         1<=5

                                                //     2<=5 true

                                                String myxpath = "//table[@id='TestCaseTable01']/tbody/tr["+i+ "]/td[" +j + "]";

                                                //                                                    /tr[2]/td[1]

                                                //                                                    /tr[2]/td[2]

                                                String val= driver.findElement(By.xpath(myxpath)).getText();

                                                System.out.println("all rows and all cells data "+val);

                                    }

                        }

 

                        System.out.println("************************");

 

                        //2nd way get all tr td list- //table[@id='TestCaseTable01']/tbody/tr/td

                        List<WebElement> allTRTDTagsList = driver.findElements(By.xpath("//table[@id='TestCaseTable01']/tbody/tr/td"));

 

                        for(int i=0;i<=allTRTDTagsList.size()-1;i++)

                        {

                                    String val = allTRTDTagsList.get(i).getText();

                                    System.out.println("way2 : get all rows and all columns data="+val);

                        }

 

 

                        //  HW  display all rows and all cells data using for each loop

 

                        //HW  display all rows and all cells data using iterator()

 

                        //                     HW  display all rows and all cells data using list iterator()

 

 

 

            }

 

}

 

 

 

 

//**************************************************

 

//HW  WAP separate method  to verify given data is available or not  in table

                        //                     / if given data is available -   method must return true else false

                       

 

 public boolean verifyGivenDataInTable(string expectedData)

{

 

//  logic

  

}

 

//                     ex: verifyGivenDataInTable("TC_01"); // true

                        // verifyGivenDataInTable("TC_9") // false

 

 

 

// HW read all data from 'Activity Code'  table

                         

Xpath for 'Verify Logout' in test case Table

--> //td[text()='Verify Logout']             - 1 ele

 

all td tags where text = 'Verify Logout'

 

 

//Verify data is available in table directly by using isDisplayed()

 

package WebTableBasics;

 

import java.util.NoSuchElementException;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class VerifyDataFromTable {

 

            public static void main(String[] args) {

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        //************************************

//                                             String expdata = "Verify Logout";

//                                             String expdata = "Verify Order creation";

                        String expdata = "Verify Order deleted";

 

                        // check  "Verify Logout" text is displayed or not

                        boolean isDisplayed;

                        try

                        {

                                     isDisplayed = driver.findElement(By.xpath("//td[text()='" +expdata+ "']")).isDisplayed();

                                    ///    if ele is not found in age, throws NosuchElement Exception but not false

 

                                                //Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table[@name='TestCaseTable']//td[text()='Verify Order deleted']"}

                                                           

                                     if(isDisplayed)

                                                {

                                                            System.out.println("Data=" +expdata +  " is displayed in test cases table");

                                                }

                                                else

                                                {

                                                            System.out.println("Data=" +expdata +  " is  not displayed in test cases table");

                                                           

                                                }

                       

                        }

                        catch (org.openqa.selenium.NoSuchElementException e) {

                                    System.out.println("NoSuch Element Exception . Data=" +expdata +  " is  not displayed in test case table");

                        }

                       

                        //                      true

                        //   isDisplayed  == true

                       

                       

                       

                       

           

 

//                    

 

 

            }

 

}

 

 

Note:  

     //tag[text()='full text']

 

xpath - any td tag contains given text = Logout  ---> 

//tag[contains(text(),'partial text')]

--------------------------------

       text contains given partial text

 xpath --> //td [contains(text(),'Logout')]

            all td tags text contains  given 'Logout'

 

      

Short cut --> text()  replace by .  (dot)

--------------------------

 

ex://table[@id='TestCaseTable01']//td[contains(text(),'Logout')]

-> //table[@id='TestCaseTable01']//td[contains(.,'Logout')]

 

ex2:text()  -->  replace by .  (dot)

//table[@id='TestCaseTable01']//td[text()='Verify Logout']

-->   //table[@id='TestCaseTable01']//td[.='Verify Logout']

 

          .  = text()

 

FAQ HW  Search google  = Crick info , open any match -  check score table  ?

 

 

https://www.espncricinfo.com/series

 

HW get scores of given batsman  name -ex:  David Warner, Virat Kohli  ?

    1. Identify 'Virat kohli'   batsman on page

    2.   Go to runs columns i.e 'R'  columns

 

(//table[@class='ds-w-full ds-table ds-table-xs ds-table-auto  ci-scorecard-table'])[1]//span[text()='Virat Kohli']/ancestor::td/following-sibling::td[2]/strong

 

 

 

change batsman name - check

(//table[@class='ds-w-full ds-table ds-table-xs ds-table-auto  ci-scorecard-table'])[1]//span[text()='Cameron Green']/ancestor::td/following-sibling::td[2]/strong

 

 

HW get all 4s of specific batsman?

HW get all 6s of specific batsman?

 

HW get all scores of all batsmans and sum it ?

 

 

Handle elements inside Table: checkbox, Textbox, Dropdown

 

 Click link inside table

 Click checkbox inside table

  Send data in Textbox inside table

  Select  Dropdown value inside table

 

 

Handling "Links" inside table:

 

package WebTableBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class LinkInsideTable {

 

//         int a; // 0 - default values to instance variable

//         float f; // 0.0

//         String sname; // null

//         boolean b;//false

            static WebDriver driver;// non sttaic var

 

            //Develop a  re-usable method- clickGivenLinkInsideActivityCodeTable("10110101");

            public static void clickGivenLinkInsideActivityCodeTable(String activityCode)

            { //                                                            activityCode=       10110101

                                    try {

 

                                    //driver.findElement(By.xpath("//table[@name='HulTable']/tbody/tr/td/a[text()='10110102']")).click();

                                             driver.findElement(By.xpath("//table[@name='HulTable']/tbody/tr/td/a[text()='" +activityCode+ "']")).click();

                                   

                        }

                        catch (NoSuchElementException e) {

                                    System.out.println(" Plz  give valid activity code. Unable to find the eleemnt with activityCode="+activityCode  );

                        }

                       

            }

           

            public static void main(String[] args) {

 

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

//                     WebDriver driver =  new ChromeDriver();

                         driver =  new ChromeDriver();

                       

                        System.out.println("Inisde main(), driver="+driver);

                       

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

                        //*******************************

 

                        //  click  '10110101' link in 'Activity Code' Table                   

                       

 

//                     /  note: we can use below any xpath

                        ////table[@id='HulTablee01']/tbody/tr[2]/td/a[text()='10110101']

                        ////table[@name='HulTable']/tbody/tr[2]//a[text()='10110101']

                        // //table[@name='HulTable']/tbody//a[text()='10110101']

//                     //table[@name='HulTable']//a[text()='10110101']

                       

                        // //table[@name='HulTable']/tbody/tr/td/a[text()='10110101']

                        // //table[@name='HulTable']/tbody/tr/td/a[text()='10110104']

//         driver.findElement(By.xpath("//table[@name='HulTable']/tbody/tr/td/a[text()='10110101']")).click();

                        // click  '10110102' link in 'Activity Code' Table        

//         driver.findElement(By.xpath("//table[@name='HulTable']/tbody/tr/td/a[text()='10110102']")).click();

                       

                        // WAP to develop a re usable  method click given link by  passing  10110101, 10110102

                       

                        //  +ve test data

//                     clickGivenLinkInsideActivityCodeTable("10110101"); // ok

                       

//                     clickGivenLinkInsideActivityCodeTable("10110102");// ok

                       

                        // -ve test data

                        clickGivenLinkInsideActivityCodeTable("10110502");

                       

// HW call clickGivenLinkInsideActivityCodeTable()  by passing 10110103,

 

//HW    call clickGivenLinkInsideActivityCodeTable()  by passing 10110104

 

 

            }

}          

 

           

Handling Table with checkboxes:

 

package WebTableBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class TableWithCheckboxes {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

//                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver   driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        //****************************

                        // click checkbox where Activity code = "10110101"

                       

            driver.findElement(By.xpath("//table[@name='HulTable']//a[text()='10110101']/parent::td/following-sibling::td[4]/input[1]")).click();

                       

                        Thread.sleep(4000);

                                   

 

                        // click checkbox whereActivity code =   10110102

            driver.findElement(By.xpath("//table[@name='HulTable']//a[text()='10110102']/parent::td/following-sibling::td[4]/input[1]")).click();

                       

                       

                        // HW click checkbox where Activity code =  10110104

                       

                        // HW click checkbox where Activity code =10110105

                       

 

                       

 

 

            }

 

}

 

------------------------

//HW Get all checboxes count in 'Activity Code'       table

                       

// HW Click all check boxes in 'Activity Code' table

 

HW Develop a reusable method to click checkbox- for given activity code

public static void clickCheckboxInActivityCodeTable(String activityCode)

            {          

           

            }

  101 - Clicks checkbox

  102 - Clicks checkbox

 

 

                        clickCheckboxInActivityCodeTable("10110101");

                        clickCheckboxInActivityCodeTable("10110102");

                        clickCheckboxInActivityCodeTable("10110105"); // display proper msg - if link is not available in table

                       

 

                        // HW  get  all link count and all link names in 'Activity Code' table

 

 

 

Handling Table With Textbox :

 

           

package WebtableBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class TableWithTextbox {

 

            public static void main(String[] args) throws InterruptedException {

                        // TODO Auto-generated method stub

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        ChromeDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        // HW  Enter 'Ram' in text box where ename  ="Ram"

                        // identify ename =  Ram , next go to text box

                       

 

                        // prefer this -one //table[@name='TextboxTable']//tr/td[contains(text(),'Swathi')]/preceding-sibling::td/input

 

                       

                        //  HW Enter 'Ramu' in text box where ename  ="Ramu"

 

                        //HW Enter 'Raju' in text box where ename  ="Raju"

 

 

            }

 

}

 

 

Handling Dropdown inside table:

 

                        // HW Select 'Suzuki'  val in dropdown where ename  = 'Swathi'

 

                        // HW Select 'Honda'  val in dropdown where ename  = 'Raju'                    

 

Get branch name based on Activity code no = 101

 

package WebTableBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class GetBranchByActivityCode {

 

            public static void main(String[] args) {

                        // TODO Auto-generated method stub

                        //                     System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo\\Downloads\\chromedriver_win32 (8)\\chromedriver.exe");

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver driver =  new ChromeDriver();

 

                        // get("url ") - to open the url in chrome browser

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/WebTableAll.html");

 

                        //*************************************************

 

                        //  get branch name based on activity cod e = 101

                        //  1. identify the ele - activity code =101

                        // 2.  from activity code 101 and go to Branch

 

                        //                     <td> Chennai </td>

                        //                     <span> RAmu  </span>

                        //                     <option>  Maruthu  </option>

                        String  branchName101 = driver.findElement(By.xpath("//table[@id='HulTablee01']//a[text()='10110101']/parent::td/following-sibling::td[2]")).getText();

 

                        System.out.println("branchName101="+branchName101); // branchName101=CHENNAI

 

                        //  get branch name based on activity code =102

 

                        String  branchName102 = driver.findElement(By.xpath("//table[@id='HulTablee01']//a[text()='10110102']/parent::td/following-sibling::td[2]")).getText();

 

                        System.out.println("branchName102="+branchName102);

                        //branchName102=MUMBAI

 

            }

 

}

 

------------------------------------

 

                        //         HW      Develop a reusable method to get branch name by passing Activity code  = 101;

                        //

                        //                     getBranchName("10110101"); // CHENNAI

                        //getBranchName("10110102");// Mumbai

                        //getBranchName("10110109");//  -ve --if data is not exist, display proper msg

                        //                       // CHENNAI

 

 

 xpath :  tr[1], tr[2]

CSS :

Syntax: tagname:nth-child(no)

 

xpath --> tr[1] 

CSS-->  tr:nth-child(1)

 

xpath --> tr[2] 

CSS-->   tr:nth-child(2)

           

xpath :  //table[@id='TestCaseTable01']/tbody/tr[1]   / =   >(child) or space (child/subchild)

CSS :   table[id='TestCaseTable01']>tbody>tr:nth-child(1)

 

navigate to tr[2]:

xpath --> //table[@id='TestCaseTable01']/tbody/tr[2]

CSS -->   table[id='TestCaseTable01']>tbody>tr:nth-child(2)

 

navigate to tr[3]

tr:nth-child(3)

 

// HW Write css Selector to navigate to tr[4] in "Test case" table

 

 

//  navigate to tr[1]   and th[1] using CSS?

 

 

 

//HW navigate tr[2]   and td[1] using CSS in Test case table?

 

 

//   get rows count using "css"?  count - tr 'tags

table[id='TestCaseTable01']>tbody>tr

 

 

            (or)

                        (Navigate directly tr tags without using tbody)

table[id='TestCaseTable01'] tr

 

 

 

 

> in  CSS- "<" can be used to navigate to child

space in CSS -  can be used navigate to   child /subchild tags

 

 

//  get columns count using CSS?

    tr[1]/th

 

table[id='TestCaseTable01']>tbody>tr:nth-child(1)>th

table[id='TestCaseTable01'] tr:nth-child(1)>th

 

table[id='TestCaseTable01'] tr>th

 

skip tr

table[id='TestCaseTable01'] th

 

----------------------

 

+  -  navigate to 1st following -sibling

Naviagte   2nd tr tag and 1st following sibling -tr tag

css:    table[id='TestCaseTable01'] tr:nth-child(2)+tr   --> 1 ele

-------------------------------------------------

 

 

  /. --> refer same tag

 

ex:

//table[@id='HulTablee01']/.  - refers same tag or ele

 

 /..  -> refers parent tag (or) ele

              Parent::tag

 

ex:

//table[@id='HulTablee01']//tr[1]/..   --navigates tbody tag

   (or)

//table[@id='HulTablee01']//tr[1]/parent::tbody

 

-------------------

   ()[1]   

//table[@name='TestCaseTable']//*[contains(text(),'Logout')]  - 3 ele

//table[@name='TestCaseTable']//*[contains(text(),'Logout')][1]

//table[@name='TestCaseTable']//*[contains(text(),'Logout')][2]

//table[@name='TestCaseTable']//*[contains(text(),'Logout')][3]

  (or)

**In My current Project

 

//table//*[contains(text(),'Ram')][1]  -- even if we use index of[1] , we got 3 ele found. Then we must use ()

 

(//table//*[contains(text(),'Ram')])[1]

(//table//*[contains(text(),'Ram')])[2]

(//table//*[contains(text(),'Ram')])[3]

 

 

(//table//*[contains(text(),'Ram')])[last()]

 

 

HW FAQ not():  write xpath to get all menu options but not  'groceries'  in amazon ?

//div[@class='nav-menu']//a[not(contains(text(),'groceries'))]

Not()

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...