Thursday, June 27, 2024

Develop "WebElement" Generic Methods:

 

Develop "WebElement" Generic Methods:

  Common methods / Generic methods/ Re-usable Methods

 

Browser Common Methods:

            Open Given Browser   -->  void openGivenbrowser(String browserName)

            open url in browser  -->  void openURL(String myurl)

            close single browser -->  void closeBrower()

            close all browser    -->  void closeAllBrowsers();

            get title            -->  String getTitle()

            getcurrent url      -->  String getCurrentUrl()  

 

Textbox Common Methods:

            enter value in text box   --> enterTextInTextbox(String xpath, String valToBeEntered)

            get value from text box   --> String getValueFromTextBox(String xpath)

            delete from text box      -->  void clearValueFromtextbox(String xpath)

 

Radio Button Common Methods

            Select radio button                 --> selectRadioBtn(String xpath)

            check radio button is selected  --> isRadioBtnSelected(String xpath);

            it is displayed or not               --> isRadiBtnDisplayed(String xpath)

            is enabled                               -->  boolean isRadioBtnEnabled(String xpath);

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

checkbox Common methods:

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

            click/ Select checkbox   --> clickCheckbox(String xpath)

            unselect checbox         --> unSelectCheckbox(String xpath)

            checkbox is selected   --> boolean isCheckBoxSelected(String xpath)

            is displayed                  -->    isElementDisplayed(String xpath);

            is is enabled or not     -->

Button Common Methods

            click button                 -->

            get button name         -->

            is displayed                 -->

            it is enabled or not      -->

 

Dropdown Common Methods:

            select some value in dropdown  -->

            get selected value in dropdown   -->

            get all dropdown values   -->

            check given value/ text is exist in dropdown   -->

            Check given value/text is not exist in dropdown -->

            get count of all values from dropdown  -->

ex:

void selectValueInDropdown(String xpath, String value);

String getSelectedValueInDropdown(String xpath);

List<String> getAllDropdownValues(String xpath);

boolean isValueExistInDropdown(String xpath, String value);

boolean isValueNotExistInDropdown(String xpath, String val

 

Multi Select Dropdown Common Methods:

            select multiple values in MultiSelect dropdown  -->

            get selected values from Multi Select dropdown -->

            get all dropdown values  from Multi Select dropdown -->

            check given value/ text is exist in Multi Select dropdown -->

            get count of all  values from Multi Select dropdown -->

ex:

void selectMultipleValuesInMultiSelectDropdown(String xpath, List<String> values);

List<String> getSelectedValuesFromMultiSelectDropdown(String xpath);

List<String> getAllDropdownValuesFromMultiSelectDropdown(String xpath);

boolean isValueExistInMultiSelectDropdown(String xpath, String value);

int getMultiSelectDropdownValuesCount(String xpath);

 

Links:

click on given link -->

Get the url of link -->

 

Ex:

void clickOnLink(String xpath);

String getUrlOfLink(String xpath);

 

Image :

Check the image is displayed -->

get Image name -->

ex:

boolean isImageDisplayed(String xpath);

String getImageName(String xpath);

 

....  etc     we can write 'N' no of Reusable /generic methods

 

// HW :   write all  method names for above actions ?

    just write some menaingful method names  only

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

 

 Actions class methods:

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

 mouse over on Given Element -->

  double click -->

 Rt click -->

 key board operation --?

Press Tab  -->

Press tab 2 or 3 times -->

press enter from k/b -->

 

void mouseOverOnElement(String xpath);

void doubleClick(String xpath);

void rightClick(String xpath);

void pressTab();

void pressTabMultipleTimes(int times);

void pressEnter();

 

 

Alert :

-----------

if alert is there,switch to alert -->

-Get msg from alert popup window -->

accept alert -->

dismiss alert  -->

 

ex:

void switchToAlert();

String getAlertMessage();

void acceptAlert();

void dismissAlert();

 

 

Frame Generic Methods:

void switchToFrameById(String frameId);

void switchToFrameByName(String frameName);

void switchToFrameByIndex(int index);

void switchToFrameByElement(String xpath);

void switchToDefaultContent();

void switchToParentFrame();

boolean isFrameDisplayed(String xpath);

 

 

//Handling Multiple Browser Windows Generic Methods

void switchToWindowByTitle(String windowTitle);

void switchToWindowByUrl(String windowUrl);

void switchToWindowByIndex(int index);

 

String getCurrentWindowHandle();

Set<String> getAllWindowHandles();

void closeCurrentWindow();

void closeWindowByTitle(String windowTitle);

void closeWindowByUrl(String windowUrl);

boolean isWindowWithTitlePresent(String windowTitle);

boolean isWindowWithUrlPresent(String windowUrl);

 

JavaScript Executor Reusable Methods:

Textbox:

void enterTextInTextboxUsingJSE(String xpath, String value);

String getTextFromTextboxUsingJSE(String xpath);

void clearTextFromTextboxUsingJSE(String xpath);

 

Radio Button

void selectRadioBtnUsingJSE(String xpath);

boolean isRadioBtnSelectedUsingJSE(String xpath);

boolean isRadioBtnDisplayedUsingJSE(String xpath);

boolean isRadioBtnEnabledUsingJSE(String xpath);

 

Checkbox:

void clickCheckboxUsingJSE(String xpath);

void unselectCheckboxUsingJSE(String xpath);

boolean isCheckBoxSelectedUsingJSE(String xpath);

boolean isCheckboxDisplayedUsingJSE(String xpath);

boolean isCheckboxEnabledUsingJSE(String xpath);

 

Button:

void clickButtonUsingJSE(String xpath);

String getButtonNameUsingJSE(String xpath);

boolean isButtonDisplayedUsingJSE(String xpath);

boolean isButtonEnabledUsingJSE(String xpath);

 

Dropdown:

void selectValueInDropdownUsingJSE(String xpath, String value);

String getSelectedValueFromDropdownUsingJSE(String xpath);

List<String> getAllDropdownValuesUsingJSE(String xpath);

boolean isValueExistInDropdownUsingJSE(String xpath, String value);

boolean isValueNotExistInDropdownUsingJSE(String xpath, String value);

int getDropdownValuesCountUsingJSE(String xpath);

 

Multi Select Dropdown

void selectMultipleValuesInMultiSelectDropdownUsingJSE(String xpath, List<String> values);

List<String> getSelectedValuesFromMultiSelectDropdownUsingJSE(String xpath);

List<String> getAllDropdownValuesFromMultiSelectDropdownUsingJSE(String xpath);

boolean isValueExistInMultiSelectDropdownUsingJSE(String xpath, String value);

int getMultiSelectDropdownValuesCountUsingJSE(String xpath);

 

Links

void clickOnLinkUsingJSE(String xpath);
String getUrlOfLinkUsingJSE(String xpath);
 

Image

boolean isImageDisplayedUsingJSE(String xpath);
String getImageNameUsingJSE(String xpath);
 

 

 

 

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

 

 

Create Package -Utilities

create class - WebUtilites :

 

            // This class contains all reusable methods  for browser , textbox, Radio btn, checkbox, Dropdown , MultiSelect dropdown

 

/*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                    :

             * Create Date                         :

             * Modified Date         :

             * Method call             : 

             *                                                                    

             *                                                                    

             * Return type             :

             */

           

 

 

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

package Utilities;

 

import java.sql.Driver;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.edge.EdgeDriver;

 

public class WebUtilites

{

//         //    ............  etc

//         int i ; // 0

//         float f; // 0.0

//         String name; //  null

//         char ch ;  // space

//         boolean b ;// false

//         static WebDriver driver; //   null

            static WebDriver driver; //  static or non static

 

 

 

            //Browser :   common methods / Generic methods:

            //-------------------------------

            //                     Open chrome, Edge browser      --> openBrowser() --> Done

            //                     open url in browser               --> openUrlInBrowser("amazon.com") --> Done

            //                     get title                                                           --> getTitleOfBrowser()  -->  Done

            //                     getcurrent url                      --> HW

            //                     close single browser               --> HW

            //                     close all browser                                 --> HW

 

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

            //                                                         Browser Common/Generic Methods

 

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

            /*

             * Method Name                     : openBrowser

             * Objective                 : it is used to open the given browser

             * Arguments              : browserType

             * Author                        :  Dharani

             * Create Date                         :  1-Jun-2024

             * Modified Date         :  NA

             * Method call             :  openBrowser("chrome");

             *                                                                    

             *                                                                    

             * Return type             :  void

             */

                       

            public static void openBrowser(String browserType)

            {  //                                     Edge browserType= Edge

                        System.out.println("Open Browser="+ browserType);

                       

                        browserType  = browserType.toLowerCase();

                        //                     Edge

                        //                    edge

                        if(browserType.equals("chrome")) // to open chrome browser

                        {//   Edge             chrome - false

                                      // selenium code to open - chrome

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

                                    // . Represents current project folder name

                                   

                                    //                                      chrome

                                    //                     //open chrome browser

//                                 WebDriver  driver =  new ChromeDriver();

                                      driver =  new ChromeDriver();

                        }

                        else if(browserType.equals("edge"))

                        {     //  Edge                 edge -- false

                                    System.out.println("in side edge ");

                                    //selenium code to open - edge

                                    System.setProperty("webdriver.edge.driver", ".\\Drivers\\msedgedriver.exe");

//                                 // . Represents current project folder name

            //

//                                 //                     //open Edge browser

//                                 WebDriver  driver =  new EdgeDriver();

                                      driver =  new EdgeDriver();

                        }

                        //   firefox

                       

                       

                        // safari

                        else

                        {

                                    System.out.println("Plz give valid browser type="+ browserType);

                        }

 

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

                        //

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

 

                       

            }

 

 

 

            /*

             * Method Name                     : openUrlInBrowser

             * Objective                 : It is used to open given url

             * Arguments              : String url

             * Author                                : Dharani

             * Create Date                         : 1-Jun-2024

             * Modified Date         : NA

             * Method call             :           openUrlInBrowser("https://www.amazon.com");                                                           

             * Return type             :

             */

            public static void openUrlInBrowser(String url)

            {

                        printMsg("Open url ="+ url);

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

                        driver.get(url);

                        // null* any method --> NullpointerException

                        //Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "Utilities.WebUtilites.driver" is null

//                     at Utilities.WebUtilites.openUrlInBrowser(WebUtilites.java:105)

            }

 

 

            /*

             * Method Name                     : getTitleOfBrowser

             * Objective                 : It is used to get title of browser

             * Arguments              : NA

             * Author                                : Dharani

             * Create Date                         : 1-Jun-2024

             * Modified Date         :  NA

             * Method call             :  getTitleOfBrowser();

             *                                                                    

             *                                                                    

             * Return type             : String

             */

            public static String  getTitleOfBrowser()

            {

                        String title = driver.getTitle();

                        printMsg("title ="+ title);

                        return title;

            }

 

 

 

 

            /*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                    :

             * Create Date                         :

             * Modified Date         :

             * Method call             : 

             *                                                                    

             *                                                                    

             * Return type             :

             */

 

 

 

 

            //------------------------

            //Textbox  common Methods:

            //---------------------

            //                     enter value in text box  -->  enterValueInTextbox()

            //                     get value from text box   --> HW

            //                     delete from text box   -->  HW

 

            /*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                    :

             * Create Date                         :

             * Modified Date         :

             * Method call             : 

             *                                                                    

             *                                                                    

             * Return type             :

             */

//         public static void enterValueInTextbox(String nameLoc,String valToBeEntered)

            public static void enterValueInTextbox(String xpathLoc,String valToBeEntered)

            { //                                    xpathLoc =     //input[@name='lastname']

//                     driver.findElement(By.name("firstname")).sendKeys("Ramu");

//                     driver.findElement(By.name(nameLoc)).sendKeys(valToBeEntered);

                        driver.findElement(By.xpath(xpathLoc)).sendKeys(valToBeEntered);

                        //                             //input[@name='lastname']

            }

 

 

 

            //----------------------------------

            //Radio button :

            //---------------

            //                     Select radio button --> HW

            //                     check radio button is selected --> HW

            //                     it is displayed or not --> HW

            //                     is enabled --> HW

 

 

            /*

             * Method Name                     : selectRadioBtn

             * Objective                 : to select given radio btn

             * Arguments              :

             * Author                                :

             * Create Date                         :

             * Modified Date         :

             * Method call             :                                                          

             * Return type             : 

             * 

             */

 

 

 

            /*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                                :

             * Create Date                         :

             * Modified Date         :

             * Method call             :                                                          

             * Return type             : 

             * 

             */

 

 

            //---------------------------------

            //checkbox Common methods:

            //----------------

            //                     click/ Select checkbox  -->  HW

            //                     unselect checbox        -->  HW

            //                     checkbox is selected    --> HW

            //                     isdisplayed    --> HW

            //                     is is enabled or not    --> HW

            //----------------------------

 

            //Button common method :

            //------------------

            //                     click button    --> HW

            //                     get button name    --> HW

            //                     isdisplayed    --> HW

            //                     is is enabled or not    --> HW

            //---------------------------------

 

 

 

            //Dropdown common methods:

            //---------------  

            //                     select some value in dropdown                     --> 

            //                     get selected value in dropdown     -->  HW

            //                     get all dropdown values                       -->  HW

            //                     check given value/ text is exist in dropdown   --- >  HW

            //                     get count values from dropdown                  -->   HW

            //--------------------------------------

 

 

 

            //Multi Select dropdown  common methods:

            //--------------------

            //                     select multiple values in MultiSelect dropdown    --> HW

            //                     get selected values from Multi Select dropdown    --> HW

            //                     get all dropdown values  from Multi Select dropdown    --> HW

            //                     check given value/ text is exist in Multi Select dropdown    --> HW

            //                     get count values from Multi Select dropdown              --> HW

            //

            //------------------------------------------

            //Links:

            //------------

            //

            //

            //-----------------

            //Image :

            //--------

 

 

 

            // Actions class methods:

 

            /*

 mouse over on Given Element     --> HW

  double click -->HW

 Rt click -->HW

 key board operation --? HW

Press Tab  -->HW

Press tab 2 or 3 times -->HW

press enter from k/b --> HW

             */

 

            //Alert :

 

            /*

if alert is there,switch to alert -->HW

-Get msg from alert popup window -->HW

accept alert -->HW

dismiss alert  --> HW

             */

 

 

 

            //HW JSE --> write all reusable method names for all elements ex: text box, Radio button,checkbox...etc

 

            public static void printMsg(String msg)

            {

                        System.out.println(msg);

            }

           

 

}

 

 

Check Generic methods are  working (or) not from Other classes:

 

 

package Utilities;

 

//  get WebUtitlies class methods inside TestUtilities

//   1. create obj for class -->

    2 . Inheritance

 

package Utilities;

 

public class TestUtilities extends WebUtilites

{// 

 

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

                        // TODO Auto-generated method stub

 

                        //  get WebUtitlies class methods inside TestUtilities

                        //   1. create obj for class -->

                        //             2 . Inheritance

 

                        // Call all methods from WebUtilities class

                        // create obj for WebUtilities class

                        WebUtilites wu =  new WebUtilites();                                              

 

                        // call openBrowser -  chrome

                        //                                             wu.openBrowser("chrome");

 

                        //                                             wu.openBrowser("Edge");// not working -- handled

                        //                                             wu.openBrowser("edge");

 

                        //   -ve scenario

                        //                                             wu.openBrowser("edge");

                        wu.openBrowser("chrome");

 

                        // HW open browser using firefox

 

                        // How to view Method  definition/Code 

                        //Pres  Ctrl +   Move mouse over on method name - Click Open declaration--> it goes to Method body directly

 

                        // call openUrlInBrowser("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

                        //                                             wu.openUrlInBrowser("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

 

                        // call openUrlInBrowser("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

                        openUrlInBrowser("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

 

                        // call getTitleOfBrowser();

                        String mytitle =           wu.getTitleOfBrowser();        

                        System.out.println("title="+mytitle);

 

                        // HW getCurrentURL

 

                        //Textbox

                        //Enter some value 'Raju' in first name textbox

                        ////input[@name='firstname']

//                     wu.enterValueInTextbox("Raju");

                        Thread.sleep(4000);

//                     wu.enterValueInTextbox("Swathi");

//                     wu.enterValueInTextbox("firstname", "Raju");

                       

                        // // Enter some value 'yadhav' in 'last name' text box 

//                     wu.enterValueInTextbox("lastname", "yadhav");

 

                        // Enter some value 'yadhav' in 'last name' text box  //input[@name='lastname']             

                        wu.enterValueInTextbox("//input[@name='lastname']", "yadhav");

                       

                        //Enter some value 'Raju' in first name textbox using xpath

                        wu.enterValueInTextbox("//input[@name='firstname']", "Raju");

 

 

                        System.out.println("ends ");

 

 

            }

 

}

 

package Utilities;

 

public class TestUtilities extends WebUtilites

{// 

 

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

                        // TODO Auto-generated method stub

 

                        //  get WebUtitlies class methods inside TestUtilities

                        //   1. create obj for class -->

                        //             2 . Inheritance

 

                        // Call all methods from WebUtilities class

                        // create obj for WebUtilities class

                        WebUtilites wu =  new WebUtilites();                                              

 

                        // call openBrowser -  chrome

                        //                                             wu.openBrowser("chrome");

 

                        //                                             wu.openBrowser("Edge");// not working -- handled

                        //                                             wu.openBrowser("edge");

 

                        //   -ve scenario

                        //                                             wu.openBrowser("edge");

                        wu.openBrowser("chrome");

 

                        // HW open browser using firefox

 

                        // How to view Method  definition/Code 

                        //Pres  Ctrl +   Move mouse over on method name - Click Open declaration--> it goes to Method body directly

 

                        // call openUrlInBrowser("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

                        //                                             wu.openUrlInBrowser("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

 

                        // call openUrlInBrowser("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

                        openUrlInBrowser("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

 

                        // call getTitleOfBrowser();

                        String mytitle =           wu.getTitleOfBrowser();        

                        System.out.println("title="+mytitle);

 

                        // HW getCurrentURL

 

                        //Textbox

                        //Enter some value 'Raju' in first name textbox

                        ////input[@name='firstname']

//                     wu.enterValueInTextbox("Raju");

                        Thread.sleep(4000);

//                     wu.enterValueInTextbox("Swathi");

//                     wu.enterValueInTextbox("firstname", "Raju");

                       

                        // // Enter some value 'yadhav' in 'last name' text box 

//                     wu.enterValueInTextbox("lastname", "yadhav");

 

                        // Enter some value 'yadhav' in 'last name' text box  //input[@name='lastname']             

//                     wu.enterValueInTextbox("//input[@name='lastname']", "yadhav");

                       

                        //Enter some value 'Raju' in first name textbox using xpath

//                     wu.enterValueInTextbox("//input[@name='firstname']", "Raju");

 

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

//                    

//                     // HW Enter some  value in 'Test Field'  and Password

//                    

//                     // - ve if xpath is wrong--  display proper msg

//                     wu.enterValueInTextbox("//input[@name='firstname123']", "Swathi");

                        //Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element:

                        //Unable to locate element: {"method":"xpath","selector":"//input[@name='firstname123']"}

                        System.out.println("Check after exception");

//                    

//                     //get val from first name textbox

//                     String valFromfirstNameTxtbox = wu.getValueFromTextbox("//input[@name='firstname']");

 

//              System.out.println("valFromfirstNameTxtbox="+valFromfirstNameTxtbox);//default value Ram Raju

//                    

//                     //get val from "last name" textbox -//input[@name='lastname']

//                     String valFromLastNameTxtbox = wu.getValueFromTextbox("//input[@name='lastname']");

 

//                     System.out.println("valFromLastNAmeTxtbox="+valFromLastNameTxtbox);

//                     //valFromLastNAmeTxtbox=yadhav

//                    

//                     //  handle -ve  if xpath -is wrong- display proper msg

//                     valFromLastNameTxtbox = wu.getValueFromTextbox("//input[@name='lastname123']");

                         //                          null

//                     System.out.println("valFromLastNAmeTxtbox="+valFromLastNameTxtbox);

//                                                                                                                                                                                             null

//                    

                        // click "Male" Radio button

//                     wu.selectRadioBtn("//input[@id='maleid']");

                       

                        // -ve if xpath id wrong -display proper msg

//                     wu.selectRadioBtn("//input[@id='maleid1233']");

                       

                        //HW  click 'Female'  radio button

                       

                        // check "male" radio btn is selected

//                     boolean maleRadioBtnSelectedStatus =  wu.isRadioBtnSelected("//input[@id='maleid']");

           

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

                       

                        //HW Get "Female" Radio button selected status

                       

                       

                        //HW Develop all reusable methods for  Chekkbox

                       

//                     HW Develop all reusable methods for  Button

                       

                       

                        //Dropdown common methods:

                       

//                     Thread.sleep(5000);

//                     // select 'Audi' value in 'cars' dropdown

//                     wu.selectGivenValueInDropdown("//select[@id='idfirst']", "Audi");

//                    

                        Thread.sleep(5000);

//                     // select 'Kia' value in 'cars' dropdown

//                     wu.selectGivenValueInDropdown("//select[@id='idfirst']", "Kia");

//                    

//                    

//                     //HW  Select 'suzuki'  in 'bikes' dropdown

//                     //select[@name='bikes']

////                  Thread.sleep(5000);

 

//                    

//                     //HW  Select 'Hero' in 'bikes' dropdown

 

                       

                        Thread.sleep(5000);

                       

                        // if xpath is wrong

//                     wu.selectGivenValueInDropdown("//select[@name='bikes123']", "Hero");

                        //Exception in thread "main" org.openqa.selenium.NoSuchElementException:

                        //no such element: Unable to locate element: {"method":"xpath","selector":"//select[@name='bikes123']"}

                       

                       

                       

                        // if given value is not there in dropdown

//                     util.selectValFromDropdown("//select[@name='bikes']", "Hero123");

                        //Exception in thread "main" org.openqa.selenium.NoSuchElementException:

                        //Cannot locate option with text: Hero123

                       

                        //                     get selected value in dropdown     -->  HW

                        //                     get all dropdown values                       -->  HW

                        //                     check given value/ text is exist in dropdown   --- >  HW

                        //                     get count values from dropdown                  -->   HW

                       

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

 

                        //Multi Select dropdown  common methods:

                        //                     select multiple values in MultiSelect dropdown       --> HW

                        //                     get selected values from Multi Select dropdown      --> HW

                        //                     get all dropdown values  from Multi Select dropdown            --> HW

                        //                     check given value/ text is exist in Multi Select dropdown            --> HW

                        //                     get count values from Multi Select dropdown          --> HW

                        //

                       

                        //------------------------------------------

                        //Links: HW

                        //------------

                        //

                        //

                        //-----------------

                        //Image : HW

                        //--------

 

 

 

                        //HW  Develop below methods for below "Actions" class methods:

 

                        /*

             mouse over on Given Element -->

              double click -->

             Rt click -->

             key board operation --?

            Press Tab  -->

            Press tab 2 or 3 times -->

            press enter from k/b -->

                         */

 

                        //Alert :

 

                        /*

            if alert is there,switch to alert -->

            -Get msg from alert popup window -->

            accept alert -->

            dismiss alert  -->

                         */

 

                        // JAvascriptExecutor Resuable Method

 

                        //HW JSE --> write all reusable method names for all elements

                        //ex: text box, Radio button,checkbox...etc

                       

                       

                        //  if we want to pass Difference  locators name, id .....

                        // Develop get_Element() return type webelement

                         //  handle code --  invalid locator format

                        wu.getElement("firstname_name").sendKeys("Ramu");

                       

                        // clear value from first name text box using getElement(name)

                        wu.getElement("firstname_name").clear();

 

                       

                        // send "Ram"  data in 'first name' textbox by passing id- getElement(id)

                         wu.getElement("idfirst_id").sendKeys("Ram");

                         //  --  invalid locator format -idfirst_idclass

                         

                         // we can follow any naming convention

                         //  name_firstname

                         // id_idfirst

                         

                         // Replace in all existing method where ever driver.findelement() is there

                         // by getelement();

                         wu.enterValueInTextbox("firstname_name", "Swathi");

                         

                         wu.enterValueInTextbox("//input[@id='idfirst'][@name='lastname']_xpath", "Swathi");

                         

                         //  HW Enter some value in first name text box:  name, id, class,xpath, cssselector

                         

//                     HW Enter some value in LAst name text box:  name, id, class,xpath, cssselector

                         

                         // If we give invalid  xpath, display proper msg

                         wu.enterValueInTextbox("firstname123_name", "Ranga");

                       

                        System.out.println("ends ");

 

 

            }

 

}

 

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

package Utilities;

 

import java.sql.Driver;

 

import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.edge.EdgeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class WebUtilites

{

//         //    ............  etc

//         int i ; // 0

//         float f; // 0.0

//         String name; //  null

//         char ch ;  // space

//         boolean b ;// false

//         static WebDriver driver; //   null

            static WebDriver driver; //  static or non static

 

 

 

            //Browser :   common methods / Generic methods:

            //-------------------------------

            //                     Open chrome, Edge browser      --> openBrowser() --> Done

            //                     open url in browser               --> openUrlInBrowser("amazon.com") --> Done

            //                     get title                                                           --> getTitleOfBrowser()  -->  Done

            //                     getcurrent url                      --> HW

            //                     close single browser               --> HW

            //                     close all browser                                 --> HW

 

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

            //                                                         Browser Common/Generic Methods

 

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

            /*

             * Method Name                     : openBrowser

             * Objective                 : it is used to open the given browser

             * Arguments              : browserType

             * Author                        :  Dharani

             * Create Date                         :  1-Jun-2024

             * Modified Date         :  NA

             * Method call             :  openBrowser("chrome");

             *                                                                    

             *                                                                    

             * Return type             :  void

             */

                       

            public static void openBrowser(String browserType)

            {  //                                     Edge browserType= Edge

                        System.out.println("Open Browser="+ browserType);

                       

                        browserType  = browserType.toLowerCase();

                        //                     Edge

                        //                    edge

                        if(browserType.equals("chrome")) // to open chrome browser

                        {//   Edge             chrome - false

                                      // selenium code to open - chrome

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

                                    // . Represents current project folder name

                                   

                                    //                                      chrome

                                    //                     //open chrome browser

//                                 WebDriver  driver =  new ChromeDriver();

                                      driver =  new ChromeDriver();

                        }

                        else if(browserType.equals("edge"))

                        {     //  Edge                 edge -- false

                                    System.out.println("in side edge ");

                                    //selenium code to open - edge

                                    System.setProperty("webdriver.edge.driver", ".\\Drivers\\msedgedriver.exe");

//                                 // . Represents current project folder name

            //

//                                 //                     //open Edge browser

//                                 WebDriver  driver =  new EdgeDriver();

                                      driver =  new EdgeDriver();

                        }

                        //   firefox

                       

                       

                        // safari

                        else

                        {

                                    System.out.println("Plz give valid browser type="+ browserType);

                        }

 

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

                        //

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

 

                       

            }

 

 

 

            /*

             * Method Name                     : openUrlInBrowser

             * Objective                 : It is used to open given url

             * Arguments              : String url

             * Author                                : Dharani

             * Create Date                         : 1-Jun-2024

             * Modified Date         : NA

             * Method call             :           openUrlInBrowser("https://www.amazon.com");                                                           

             * Return type             :

             */

            public static void openUrlInBrowser(String url)

            {

                        printMsg("Open url ="+ url);

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

                        driver.get(url);

                        // null* any method --> NullpointerException

                        //Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "Utilities.WebUtilites.driver" is null

//                     at Utilities.WebUtilites.openUrlInBrowser(WebUtilites.java:105)

            }

 

 

            /*

             * Method Name                     : getTitleOfBrowser

             * Objective                 : It is used to get title of browser

             * Arguments              : NA

             * Author                                : Dharani

             * Create Date                         : 1-Jun-2024

             * Modified Date         :  NA

             * Method call             :  getTitleOfBrowser();

             *                                                                    

             *                                                                    

             * Return type             : String

             */

            public static String  getTitleOfBrowser()

            {

                        String title = driver.getTitle();

                        printMsg("title ="+ title);

                        return title;

            }

 

 

 

 

            /*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                    :

             * Create Date                         :

             * Modified Date         :

             * Method call             : 

             *                                                                    

             *                                                                    

             * Return type             :

             */

 

//         getElement("firstname_name");

//         getElement("idfirst_id");

            //getElement("idfirst_class");

 

            public static WebElement getElement(String locator)

            {  //                          firstname_name  i.e locator ="firstname_name"

                        //                                                   idfirst_cssSelector

//                     driver.findElement(By.name(xpathLoc));

//                     driver.findElement(By.id(xpathLoc));

//                     driver.findElement(By.xpath(xpathLoc));

                       

                         WebElement ele = null;

                        try {

                                    String sarr[] = locator.split("_");

                                    //  sarr[0] = firstname

                                    // sarr[1] == name

                                    String locVal = sarr[0];           

                                   

                                    if(locator.contains("_name"))

                                    {

                                      ele =  driver.findElement(By.name(locVal));

                                    }

                                    else if(locator.contains("_id"))

                                    {

                                                ele = driver.findElement(By.id(locVal));

                                    }

                                    else if(locator.contains("_class"))

                                    {

                                                ele = driver.findElement(By.className(locVal));

                                    }          

                                    else if(locator.contains("_xpath"))

                                    {

                                                ele = driver.findElement(By.xpath(locVal));

                                    }          

                                    else if(locator.contains("_css"))

                                    {

                                                ele = driver.findElement(By.cssSelector(locVal));

                                    }          

                                                            //  if no locator is matching

                                    else

                                    {

                                                System.out.println("You gave locator=" +locator+".Plz give valid locator format ex:  _name,_id, _class,_xpath,_css...etc");

                                    }

 

                        }

                        catch (Exception e) {

                                    System.out.println("Unable to find the ele by using locator ="+locator);

                        }

                                                           

                        return ele;

            }

 

 

            //------------------------

            //Textbox  common Methods:

            //---------------------

            //                     enter value in text box  -->  enterValueInTextbox()

            //                     get value from text box   -->

            //                     delete from text box   -->  HW

 

            /*

             * Method Name                     :

             * Objective                 :

             * Arguments              :

             * Author                    :

             * Create Date                         :

             * Modified Date         :

             * Method call             : 

             *                                                                    

             *                                                                    

             * Return type             :

             */

//         public static void enterValueInTextbox(String nameLoc,String valToBeEntered)

            public static void enterValueInTextbox(String xpathLoc,String valToBeEntered)

            { //                                    xpathLoc =     //input[@name='lastname']

//                     driver.findElement(By.name("firstname")).sendKeys("Ramu");

//                     driver.findElement(By.name(nameLoc)).sendKeys(valToBeEntered);

//                     driver.findElement(By.xpath(xpathLoc)).sendKeys(valToBeEntered);

                        //                             //input[@name='lastname']

                       

                        try {

//                                 driver.findElement(By.xpath(xpathLoc)).sendKeys(valToBeEntered);

                                    getElement(xpathLoc).sendKeys(valToBeEntered);

                                    // null * any method  --> Null pointer exception

                        }

                        catch (NoSuchElementException nse) {

                                    System.out.println("Unable to find the element in the page.Plz Give valid xpath locator ="+ xpathLoc);

                                   

                        }

                       

            }

 

 

            /*

             * Method Name                     : getValueFromTextbox

             * Objective                 : It is used to get value from given textbox

             * Arguments              : String myXpath

             * Author                        : Sai

             * Create Date                         : 3-Jun-2024

             * Modified Date         : NA

             * Method call             :  getValueFromTextbox("//input[@name='firstname']");

             *                                                                     getValueFromTextbox("//input[@name='lastname']");

             *                                                                    

             * Return type             : String

             */

            public static String getValueFromTextbox(String myXpath)

            {

                        String val = null;

                        try {

//                                 val =driver.findElement(By.xpath(myXpath)).getAttribute("value");

                                     val =getElement(myXpath).getAttribute("value");

                        }

                        catch (NoSuchElementException e) {

                                    System.out.println("Unable to find the element in the page.Plz Give valid xpath locator ="+ myXpath);

                        }

                        printMsg("val from text box="+ val);

                        return val;

                        //      null

            }

 

 

            //----------------------------------

            //Radio button :

            //---------------

            //                     Select radio button -->  Done

            //                     check radio button is selected -->  Done

            //                     it is displayed or not --> HW

            //                     is enabled --> HW

 

 

            /*

             * Method Name                     : selectRadioBtn

             * Objective                 : to select given radio btn

             * Arguments              :

             * Author                                :

             * Create Date                         :

             * Modified Date         :

             * Method call             :                                                          

             * Return type             :  void

             * 

             */

            public static void selectRadioBtn(String myxpath)

            {

                        try {

//                                 WebElement radioBtnEle=  driver.findElement(By.xpath(myxpath));

                                    WebElement radioBtnEle=  getElement(myxpath);

                                    // if radio button is already selected

                                    if(radioBtnEle.isSelected())

                                    {

                                                //  dont click radio button

                                                printMsg("Raiod button is already selected");

                                    }

                                    else

                                    { //  radio button is not selected

                                                printMsg("Raiod button is not selected");

                                                System.out.println("click Raio button ="+ myxpath);

//                                             driver.findElement(By.xpath(myxpath)).click();

                                                getElement(myxpath).click();

                                    }

                                   

                        }

                        catch (Exception e) {

                                    System.out.println("Unable to click radio button.Plz Give valid xpath locator ="+ myxpath);

                        }

                       

            }

 

 

            /*

             * Method Name                     : isRadioBtnSelected()

             * Objective                 :

             * Arguments              :

             * Author                                :

             * Create Date                         :

             * Modified Date         :

             * Method call             :                                                          

             * Return type             : 

             * 

             */

 

            public static boolean isRadioBtnSelected(String myXpath)

            {

//                     boolean isSelected = driver.findElement(By.xpath(myXpath)).isSelected();

                        boolean isSelected = getElement(myXpath).isSelected();

                       

                        printMsg("isSelected="+isSelected);

                        if(isSelected == true)

                        {

                                    System.out.println(myXpath +"Radio button is selected");

                        }

                        else

                        {

                                    System.out.println(myXpath +"Radio button is not selected");

                        }

                        return isSelected;

            }

 

            //---------------------------------

            //checkbox Common methods:

            //----------------

            //                     click/ Select checkbox  -->  HW

            //                     unselect checbox        -->  HW

            //                     checkbox is selected    --> HW

            //                     isdisplayed    --> HW

            //                     is is enabled or not    --> HW

            //----------------------------

 

            //Button common method :

            //------------------

            //                     click button    --> HW

            //                     get button name    --> HW

            //                     isdisplayed    --> HW

            //                     is is enabled or not    --> HW

            //---------------------------------

 

 

 

            //Dropdown common methods:

            //---------------  

            //                     select some value in dropdown                     --> selectGivenValueInDropdown()

            //                     get selected value in dropdown     -->  HW

            //                     get all dropdown values                       -->  HW

            //                     check given value/ text is exist in dropdown   --- >  HW

            //                     get count values from dropdown                  -->   HW

            //--------------------------------------

            public static void selectGivenValueInDropdown(String myxpath, String valToBeSelected)

            {

                        // class - Select

                        try {

//                                 WebElement dropdownEle=  driver.findElement(By.xpath(myxpath));

                                    WebElement dropdownEle= getElement(myxpath);

                                    Select sel = new Select(dropdownEle);

                                    System.out.println("Selecting Val ="+ valToBeSelected + " in dropdown");

                                    sel.selectByVisibleText(valToBeSelected);

                        } catch (Exception e) {

                                    System.out.println("Unable to find dropdown.Plz Give valid xpath locator ="+ myxpath);

                        }

                       

            }

 

 

            //Multi Select dropdown  common methods:

            //--------------------

            //                     select multiple values in MultiSelect dropdown    --> HW

            //                     get selected values from Multi Select dropdown    --> HW

            //                     get all dropdown values  from Multi Select dropdown    --> HW

            //                     check given value/ text is exist in Multi Select dropdown    --> HW

            //                     get count values from Multi Select dropdown              --> HW

            //

            //------------------------------------------

            //Links:

            //------------

            //

            //

            //-----------------

            //Image :

            //--------

 

 

 

            // Actions class methods:

 

            /*

 mouse over on Given Element     --> HW

  double click -->HW

 Rt click -->HW

 key board operation --? HW

Press Tab  -->HW

Press tab 2 or 3 times -->HW

press enter from k/b --> HW

             */

 

            //Alert :

 

            /*

if alert is there,switch to alert -->HW

-Get msg from alert popup window -->HW

accept alert -->HW

dismiss alert  --> HW

             */

 

 

 

            //HW JSE --> write all reusable method names for all elements ex: text box, Radio button,checkbox...etc

 

            public static void printMsg(String msg)

            {

                        System.out.println(msg);

            }

           

           

           

 

}

 

 

 

Define getElement():

 

 

//         getElement("firstname_name");

//         getElement("idfirst_id");

            //getElement("idfirst_class");

 

            public static WebElement getElement(String locator)

            {  //                          firstname_name  i.e locator ="firstname_name"

                        //                                                   idfirst_cssSelector

//                     driver.findElement(By.name(xpathLoc));

//                     driver.findElement(By.id(xpathLoc));

//                     driver.findElement(By.xpath(xpathLoc));

                       

                         WebElement ele = null;

                        try {

                                    String sarr[] = locator.split("_");

                                    //  sarr[0] = firstname

                                    // sarr[1] == name

                                    String locVal = sarr[0];           

                                   

                                    if(locator.contains("_name"))

                                    {

                                      ele =  driver.findElement(By.name(locVal));

                                    }

                                    else if(locator.contains("_id"))

                                    {

                                                ele = driver.findElement(By.id(locVal));

                                    }

                                    else if(locator.contains("_class"))

                                    {

                                                ele = driver.findElement(By.className(locVal));

                                    }          

                                    else if(locator.contains("_xpath"))

                                    {

                                                ele = driver.findElement(By.xpath(locVal));

                                    }          

                                    else if(locator.contains("_css"))

                                    {

                                                ele = driver.findElement(By.cssSelector(locVal));

                                    }          

                                                            //  if no locator is matching

                                    else

                                    {

                                                System.out.println("You gave locator=" +locator+".Plz give valid locator format ex:  _name,_id, _class,_xpath,_css...etc");

                                    }

 

                        }

                        catch (Exception e) {

                                    System.out.println("Unable to find the ele by using locator ="+locator);

                        }

                                                           

                        return ele;

            }

 

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...