03 Selenium

Selenium

Selenium is an open-source and a portable automated software testing tool for

testing web applications. It has capabilities to operate across different browsers

and operating systems. Selenium is not just a single tool but a set of tools that

helps testers to automate web-based applications more efficiently.

Let us now understand each one of the tools available in the Selenium suite and

their usage. 



Tool 

Description 

Selenium IDE

Selenium Integrated Development Environment (IDE) is a Firefox plugin

that lets testers to record their actions as they follow the workflow that they

need to test.

Selenium RC 

Selenium Remote Control (RC) was the flagship testing framework that

allowed more than simple browser actions and linear execution. It makes

use of the full power of programming languages such as Java, C#,

PHP, Python, Ruby, and PERL to create more complex tests.

Selenium WebDriver

Selenium WebDriver is the successor to Selenium RC which sends

commands directly to the browser and retrieves results.

Selenium Grid

Selenium Grid is a tool used to run parallel tests across different

machines and different browsers simultaneously which results

in minimized execution time.


Popular automation frameworks


Frome work

Description 

Data Driven

Data Driven framework is used to drive test cases and suites from an external

data feed. The data feed can be data sheets like xls, xlsx, and csv files.

A Data Driven Framework in Selenium is a technique of separating

the “data set” from the actual “test case” (code).

(properties, excel, xml, json, csv, edi etc.

Keyword driven

Keyword Driven Framework in Selenium is a method used for speeding up

automated testing by separating keywords for common set of functions and

instructions. All the operations and instructions to be performed are written

in some external file like an Excel sheet/ALM/(any kind of file system).

Hybrid driven

FrontEnd, BackEnd and Database – what sir said

Hybrid Driven Framework in Selenium is a concept where we are using the

advantage of both Keyword driven framework as well as Data driven framework.

It is an easy to use framework which allows manual testers to create test cases by

just looking at the keywords, test data and object repository without coding in the

framework.--And what i google it

Testing 

FE 

MT

BE

Menual 

browser

API :Rest/Soap or Postman/sopeUI 

Json,XML/XMl

SQL

Automation

Selenium

Cypress

playwrite

Java Rest-Assured

Rest-Sharp

JDBC



https://github.com/techfios-git/TestNGProjectJune23.git

Note : 

    driverpath for windows like → “D:\\myfile\\driver\\chromedriver.exe” →  double backslash  and .exe

   driverpath for mac like → “D:/myfile/driver/chromedriver”  → single forward slash and no .exe

Browser

System.setProperty(key, value)

Instance of Driver

Chrome

Key:  webdriver.chrome.driver 

Value:driverpath)

WebDriver driver = new ChromeDriver();

Firefox

Key:webdriver.gecko.driver”

Value:driverpath

WebDriver driver = new FirefoxDriver();

Microsoft Edge

Key: “webdriver.edge.driver ”

Value:driverpath

WebDriver driver = new EdgeDriver();

Opera

Key: "webdriver.opera.driver"

Value:driverpath

WebDriver driver = new OperaDriver();

Internet

Key: “webdriver.ie.driver ”

Value:driverpath

WebDriver driver = new InternetExplorerDriver();

Safari 

Key: “"webdriver.safari.driver" ”

Value:driverpath

WebDriver driver = new SafariDriver();



System.setProperty(key, value); OR System.setProperty(properties, propertiseValue);

driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS);


Note 

======================

Selenium older( <4.6) version

WDM supporting CFT 

  1. Download CFT (Cromedriver.exe ) 

→ Sysemt.setProperty(Key, Value)

  1. WDM latest version → automate download chromedriver.exe

→ add dependency

https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager/5.2.3

<dependency>

    <groupId>io.github.bonigarcia</groupId>

    <artifactId>webdrivermanager</artifactId>

    <version>5.2.3</version>

</dependency>


→ WebDriverManager.chromedriver().setup();


Selenium later( >4.6) version

After 4.6 there is an inbuild WebDriverManager in Selenium so do need to download the driver

and set property. We just need to create an instance of the driver class 

(suggest going with later than the selenium 4.6 version) 




A command refers to what Selenium has to do and the commands in Selenium are of three types:  Actions  Accessors  Assertions


A command refers to what Selenium has to do and the commands in Selenium are of three types:  

Actions 

Accessors

Assertions


Actions 


command/syntax

Description

click (locator)

Clicks on a link, button, checkbox or radio button

clickAt (locator, coordString)

Clicks on an element with the help of locator and co- ordinates

close ()

Simulates the user clicking the "close" button in the title bar of a popup window or tab.

contextMenuAt (locator, coordString)

Simulates opening the context menu of the specified element from a specified location

doubleClick (locator)

Double clicks on a webelement based on the specified element.

dragAndDrop (locator, movementsString)

Drags an element and then drops it based on specified distance.

dragAndDropToObject (Dragobject, dropobject)

Drags an element and drops it on another element.

Echo (message)

Prints the specified message on console which is used for debugging.

fireEvent (locator,eventName)

Explicitly simulate an event, to trigger the corresponding "onevent" handler

focus (locator)

Move the focus to the specified element

highlight (locator)

Changes the background color of the specified element to yellow which is useful for

debugging purposes.

mouseDown (locator)

Simulates a user pressing the left mouse button on the specified element.

mouseDownAt (locator, coordString)

Simulates a user pressing the left mouse button at the specified location on the specified element.

mouseUp (locator)

Simulates the event that occurs when the user releases the mouse button

mouseUpAt (locator, coordString)

Simulates the event that occurs when the user releases the mouse button at the specified location.

open (url)

Opens a URL in the specified browser and it accepts both relative and absolute URLs.

openWindow (url, windowID)

Opens a popup window. After opening the window, user need to activate it using the selectWindow

command.

pause (waitTime)

Waits for the specified amount of time (in milliseconds)

refresh()

Simulates the user clicking the "Refresh" button on their browser.

select (selectLocator, optionLocator)

Select an option from a drop-down using an option locator.

selectWindow (windowID)

Selects a popup window using a window locator; once a popup window has been selected,

all focus shifts to that window.

store (expression, variableName)

The name of a variable in which the result is to be stored and expression is the value to store.

type (locator, value)

Sets the value of an input field, similar to user typing action.

typeKeys (locator, value)

Simulates keystroke events on the specified element, as though you typed the value key-by-key.

waitForCondition (script, timeout)

Executes the specified JavaScript snippet repeatedly until it evaluates to "true".

waitForPageToLoad (timeout)

Waits for a new page to load.

waitForPopUp (windowID, timeout)

Waits for a popup window to appear and load.

windowFocus()

Gives focus to the currently selected window

windowMaximize()

Resize the currently selected window to take up the entire screen


Accessors                                                  

Accessors evaluate the state of the application and store the results in a variable which is used in assertions.

For example, "storeTitle".

The following table lists the Selenium accessors that are used very frequently, however the list is not exhaustive.



Command/Syntax



Command/Syntax


assertErrorOnNext (message)

Pings Selenium to expect an error on the

next command execution with an expected message.

storeAllButtons (variableName)

Returns the IDs of all buttons on the page.

storeAllFields (variableName)

Returns the IDs of all input fields on the page.

storeAllLinks (variableName)

Returns the IDs of all links on the page.

storeAllWindowIds (variable Name)

Returns the IDs of all windows that the browser

knows about in an array.

storeAllWindowTitles (variableName)

Returns the names of all windows that the browser

knows about in an array.

storeAllWindowNames (variableName)

Returns the titles of all windows that the browse

r knows about in an array.

storeAttribute (attributeLocator, variableName)

Gets the value of an element attribute. The value of the attribute may differ across browsers.

storeBodyText (variableName)

Gets the entire text of the page.

storeConfirmation (variableName)

Retrieves the message of a JavaScript confirmation

dialog generated during the previous action.

storeElementIndex (locator, variableName)

Get the relative index of an element to its parent

(starting from 0).

storeLocation (variableName)

Gets the absolute URL of the current page.

storeSelectedIds (selectLocator, variableName)

Gets all element IDs for selected options in the

specified select or multi- select element.

storeSelectedIndex (selectLocator, variableName)

Gets index (option number, starting at 0) for selected

option in the specified select element.

storeSelectedLabel (selectLocator, variableName)

Gets label (visible text) for selected option in the

specified select element..

storeSelectedValue (selectLocator, variableName)

Gets value (value attribute) for selected option in the

specified select element.

storeSelectOptions (selectLocator, variableName)

Gets all labels in the specified select drop- down.

storeTable (tableCellAddress, variableName)

Gets the text from a cell of a table. The cellAddress

syntax: tableLocator.row.column, where row and

column start at 0.

storeText (locator, variableName)

Gets the text of an element. This works for any element

that contains text.

storeTitle (variableName)

Gets the title of the current page.

storeValue (locator, variableName)

Gets the (whitespace- trimmed) value of an input

storeChecked (locator, variableName)

Gets whether a toggle- button (checkbox/radio) is

checked.

storeElementPresent (locator, variableName)

Verifies that the specified element is somewhere on

the page.

storeTextPresent (pattern, variableName)

Verifies that the specified text pattern appears

somewhere on the rendered page shown to the user.

storeVisible (locator, variableName)

Determines if the specified element is visible.


Assertions

Assertions enable us to verify the state of an application and compares against the expected. It is used in 3 modes,

viz. - "assert", "verify", and "waitfor".

For example, "verify if an item from the dropdown is selected".

The following table lists the Selenium assertions that are used very frequently, however the list is not exhaustive.


Command/syntax

description

waitForErrorOnNext (message)

Waits for error; used with the accessor assertErrorOnNext.

verifySelected (selectLocator, optionLocator)

Verifies that the selected option of a drop-down satisfies the optionSpecifier.

waitForSelected (selectLocator, optionLocator)

Waits for getting the option selected; used with the accessor assertSelected.

waitForNotSelected (selectLocator, optionLocator)

Waits for not getting the option selected; used with the accessor assertSelected.

verifyAlert (pattern)

Verifies the alert text; used with the accessor storeAlert.

waitForAlert (pattern)

Waits for the alert; used with the accessor storeAlert.

verifyAllButtons (pattern)

Verifies the button; used with the accessor storeAllButtons.

waitForAllButtons (pattern)

Waits for the button to load; used with the accessor storeAllButtons.

verifyAllLinks (pattern)

Verifies all links; used with the accessor storeAllLinks.

waitForAllLinks (pattern)

Waits for all links; used with the accessor storeAllLinks.

verifyAllWindowIds (pattern)

Verifies the window id; used with the accessor storeAllWindowIds.

waitForAllWindowIds (pattern )

Waits the window id; used with the accessor storeAllWindowIds.

verifyAttribute (attributeLocator, pattern)

Verifies an attribute of an element; used with the accessor storeAttribute.

waitForAttribute (attributeLocator, pattern)

Waits for an attribute of an element; used with the accessor storeAttribute.

verifyBodyText(pattern)

Verifies the body text; used with the accessor storeBodyText.

waitForBodyText(pattern)

Waits for the body text; used with the accessor storeBodyText.

waitForConfirmation(pattern)

Waits for confirmation; used with the accessor store ConfirmationPresent


Locators

Element Locators help Selenium to identify the HTML element the command refers to. All these locators can be identified with the help of FirePath and FireBug plugin of Mozilla. Please refer the Environment Setup chapter for details.

 identifier=id  Select the element with the specified "id" attribute and if there is no match, select the first element whose @name attribute is id.

 id=id      Select the element with the specified "id" attribute.

 name=name Select the first element with the specified  "name"  attribute

dom=javascriptExpression Selenium finds an element by evaluating the specified string that allows us to traverse through the HTML Document Object Model using JavaScript. Users cannot return a value but can evaluate as an expression in the block.

xpath=xpathExpression Locate an element using  an  XPath  expression.

 link=textPattern      Select the link element (within anchor tags) which contains text matching the specified pattern.

css=cssSelectorSyntax     Select the element using css selector.


WebDriver 

WebDriver is a tool for automating testing web applications. It is popularly known as Selenium 2.0. WebDriver uses a different underlying framework, while Selenium RC uses JavaScript Selenium-Core embedded within the browser which has got some limitations. WebDriver interacts directly with the browser without any intermediary, unlike Selenium RC that depends on a server. It is used in the following context:  Multi-browser testing including improved functionality for browsers which is not well-supported by Selenium RC (Selenium 1.0).  Handling multiple frames, multiple browser windows, popups, and alerts.  Complex page navigation.  Advanced user navigation such as drag-and-drop.  AJAX-based UI elements. 


Most Used Commands

 The following table lists some of the most frequently used commands in WebDriver along with their syntax. 



Command

Description

driver.get("URL")

To navigate to an application.

element.sendKeys("inputtext")

Enter some text into an input box.

element.clear()

Clear the contents from the input box.

select.deselectAll()

Deselect all OPTIONs from the first SELECT on the page.

select.selectByVisibleText("some text")

Select the OPTION with the input specified by the user.

driver.switchTo().window("windowName")

Move the focus from one window to another.

driver.switchTo().frame("frameName")

Swing from frame to frame.

driver.switchTo().alert()

Helps in handling alerts.

driver.navigate().to("URL")

Navigate to the URL.

driver.navigate().forward()

To navigate forward.

driver.navigate().back()

To navigate back.

driver.close()

Closes the current browser associated with the driver.

driver.quit()

Quits the driver and closes all the associated window of that driver.

driver.refresh()

Refreshes the current page.



Locatier

Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.

  • findElement() returns a WebElement object based on a specified search criteria or ends up throwing an exception if it does not find any element matching the search criteria.


  • findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.

The following table lists all the Java syntax for locating elements in Selenium WebDriver.



Method

Syntax

Description

By ID

driver.findElement(By.id(<element ID>))

Locates an element using the ID attribute

By name

driver.findElement(By.name(<element name>))

Locates an element using the Name attribute

By class name

driver.findElement(By.className(<element class>))

Locates an element using the Class attribute

By tag name

driver.findElement(By.tagName(<htmltagname>))

Locates an element using theHTML tag

By link text

driver.findElement(By.linkText(<linktext>))

Locates a link using link text

By partial link text

driver.findElement(By.partialLinkText(<linktext>))

Locates a link using the link's partial text

By CSS

driver.findElement(By.cssSelector(<css selector>))

Locates an element using the CSS selector

By XPath

driver.findElement(By.xpath(<xpath>))

Locates an element using XPath query



Interactions 

User Interactions

Selenium WebDriver is the most frequently used tool among all the tools available in the Selenium tool set. Therefore it is important to understand how to use Selenium to interact with web apps. In this module, let us understand how to interact with GUI objects using Selenium WebDriver.

We need to interact with the application using some basic actions or even some advanced user action by developing user-defined functions for which there are no predefined commands.

Listed below are the different kinds of actions against those GUI object

  • Text Box Interaction

  •  Radio Button Selection

  •  Check Box Selection

  • Drop Down Item Selection

  • Synchronization

  • Drag & Drop

  • Keyboard Actions

  • Mouse Actions

  • Multi Select

  • Find All Links




  1. Text Box Interaction

In this section, we will understand how to interact with text boxes. We can put values into a text box using the 'sendkeys' method. Similarly, we can also retrieve text from a text box using the getattribute("value") command. Take a look at the following example.

Enter  value  

->driver.findElement(By.id("cpar1")).sendKeys("10");

Get  text 

->driver.findElement(By.id("cpar1")).getAttribute("value");





  1. Radio Button Interaction

In this section, we will understand how to interact with Radio Buttons. We can select a radio button option using the 'click' method and unselect using the same 'click' method.

//  Click  on  Radio  Button driver.findElement(By.id("cpayoff1")).click();

Selected

->driver.findElement(By.id("cpayoff1")).isSelected());

IsEnabled 

->driver.findElement(By.id("cpayoff1")).isEnabled());

IsDisplayed  

->driver.findElement(By.id("cpayoff1")).isDisplayed());


  1. Check Box Interaction

In this section, we will understand how to interact with Check Box. We can select a check box using the 'click' method and uncheck using the same 'click' method.

Click  on  check  box 

->driver.findElement(By.id("caddoptional")).click();

 IsSelected

->driver.findElement(By.id("caddoptional")).isSelected());

IsEnabled

->driver.findElement(By.id("caddoptional")).isEnabled());

IsDisplayed 

->driver.findElement(By.id("caddoptional")).isDisplayed());




  1. Dropdown Interaction

In this section, we will understand how to interact with Dropdown Boxes. We can select an option using 'selectByVisibleText' or 'selectByIndex' or 'selectByValue' methods.


//  Selecting  an  item  from  Drop  Down  list  Box 

->Select  dropdown  =new  Select(driver.findElement(By.id("ccompound")));

->dropdown.selectByVisibleText("continuously")

//  you  can  also  use  dropdown.selectByIndex(1)  to

//  select  second  element  as  index  starts  with  0.

//  You  can  also  use  dropdown.selectByValue("annually")

driver.findElement(By.id("ccompound")).isSelected())

driver.findElement(By.id("ccompound")).isEnabled())

driver.findElement(By.id("ccompound")).isDisplayed()

  1. Drag & Drop

As a tester, you might be in a situation to perform a 'Drag & drop' operation. We will perform a drag and drop operation by picking up a tree grid that is available for us on 

WebElement  From  = driver.findElement(By.xpath(".//*[@id='j3_7']/a"));

WebElement  To  = driver.findElement(By.xpath(".//*[@id='j3_1']/a"));


Actions  builder  =  new  Actions(driver);

Action  dragAndDrop  =  builder.clickAndHold(From)

.moveToElement(To)

.release(To)

.build();

 dragAndDrop.perform();





  1. Keyboard Actions

Given below are the methods to perform keyboard actions:

sendKeys - Sends keys to the keyboard representation in the browser. Special keys that are not text, represented as Keys are recognized both as part of sequences of characters, or individually.

pressKey - Press a key on the keyboard that is NOT text. The keys such as function keys "F1", "F2", "Tab", "Control", etc. If keyToPress is a sequence of characters, different driver implementations may choose to throw an exception or to read only the first character in the sequence.

releaseKey - Release a key on the keyboard after executing the keypress event. It usually holds good for non-text characters.

Here are the syntax to call keyboard functions using Selenium WebDriver.

void  sendKeys(java.lang.CharSequence  keysToSend)

void  pressKey(java.lang.CharSequence  keyToPress)

void  releaseKey(java.lang.CharSequence  keyToRelease)



  1. Mouse Actions

Listed below are some of the key mouse actions that one would come across in most of the applications:

•Click -

Performs a Click. We can also perform  a click based on coordinates.

•contextClick

Performs a context click/right-click on an element or based on the coordinates.

•doubleClick 

Performs a double-click on the webelement or based on the coordinates. If left empty, it performs double-click on the current location.

•mouseDown

 Performs a mouse-down action on an element or based on coordinates.

•mouseMove 

Performs a mouse-move action on an element or based on coordinates.

•mouseUp 

Releases the mouse usually followed by mouse-down and acts based on coordinates.

Here are the syntax to call mouse actions using Selenium WebDriver:

void  click(WebElement  onElement)

void  contextClick(WebElement  onElement) 

void  doubleClick(WebElement  onElement

void  mouseDown(WebElement  onElement) 

void  mouseUp(WebElement  onElement)  

void  mouseMove(WebElement  toElement)

void  mouseMove(WebElement  toElement,  long  xOffset,  long  yOffset)





  1. Multi Select Action

Sometimes we would be in a situation to select two or more items in a list box or text area. To understand the same, we would demonstrate multiple selection from the list using 

driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();

driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();

Or

//  Perform  Multiple  Select

Actions  builder  =  new  Actions(driver); 

WebElement  select =  driver.findElement(By.id("ContentHolder_lbFeatures_LBT)); 

List<WebElement> options =  select.findElements(By.tagName("td")); 


System.out.println(options.size());


Action  multipleSelect  =  builder.keyDown(Keys.CONTROL)

.click(options.get(2))

.click(options.get(4))

.click(options.get(6))

.build(); 

multipleSelect.perform();


  1. Find All Links

Testers might be in a situation to find all the links on a website. We can easily  do so by finding all elements with the Tag Name "a", as we know that for any link reference in HTML, we need to use "a" (anchor) tag.


List<WebElement>  links  = driver.findElements(By.tagName("a"));

 System.out.println("Number  of  Links  in  the  Page  is  "  + links.size());

for  (int  i  =  1;  i<=links.size();  i=i+1)

{

System.out.println("Name  of  Link#  "  +  i  -  + links.get(i).getText());

}



Synchronization

To synchronize between script execution and application, we need to wait after performing appropriate actions. Let us look at the ways to achieve the same.


Thread.Sleep

Thread.Sleep is a static wait and it is not a good way to use in scripts, as it is sleep without condition.

Thread.Sleep(1000);  //Will  wait  for  1  second.



Explicit Waits

An 'explicit wait' waits for a certain condition to occur before proceeding further. It is mainly used when we want to click or act on an object once it is visible.


WebDriver  driver  =  new  FirefoxDriver(); driver.get("Enter  an  URL"S);

WebElement  DynamicElement  =  (new  WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("DynamicEle ment")));


Implicit Wait

Implicit wait is used in cases where the WebDriver cannot locate an object immediately because of its unavailability. The WebDriver will wait for a specified implicit wait time and it will not try to find the element again during the specified time period.

Once the specified time limit is crossed, the WebDriver will try to search the element once again for one last time. Upon success, it proceeds with the execution; upon failure, it throws an exception.

It is a kind of global wait which means the wait is applicable for the entire driver. Hence, hardcoding this wait for longer time periods will hamper the execution time.


WebDriver  driver  =  new  FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS); driver.get("Enter  an  URL");

WebElement  DynamicElement  =  driver.findElement(By.id("DynamicElement"));



Fluent Wait

A FluentWait instance defines the maximum amount of time to wait for a condition to take place, as well as the frequency with which to check the existence of the object condition.

Let us say we will 60 seconds for an element to be available on the page, but we will check its availability once in every 10 seconds.


Wait  wait  =  new  FluentWait(driver)

.withTimeout(60,  SECONDS)

.pollingEvery(10,  SECONDS)

.ignoring(NoSuchElementException.class);

WebElement  dynamicelement  =  wait.until(new Function<webdriver,webElement>(){

public  WebElement  apply(WebDriver  driver){

return  driver.findElement(By.id("dynamicelement"));

}

}

);


Test design Techniques 


There are various components involved in designing the tests. Let us understand some of the important components involved in designing a framework as well. We will learn the following topics in this chapter:

Page Object Model

Parameterizing using Excel

Log4j Logging

Exception Handling

Multi Browser Testing

Capture Screenshots

Capture Videos



Page Object Model

Selenium acts on webelements with the help of their properties such ID, name, XPath, etc. Unlike QTP which has an inbuilt object repository (OR), Selenium has no inbuilt ORs.

Hence we need to build an OR which should also be maintainable and accessible on demand. Page Object Model (POM) is a popular design pattern to create an Object Repository in which each one of those web-elements properties are created using a class file.

Advantages

•POM is an implementation where test objects and functions are separated from each other, thereby keeping the code clean.

•The objects are kept independent of test scripts. An object can be accessed by one or more test scripts, hence POM helps us to create objects once and use them multiple times.

Since objects are created once, it is easy to access as well as update a particular property of an object.


POM Flow Diagram

Objects are created for each one of the pages and methods are developed exclusively to access to those objects. Let us use http://calculator.net for understanding the same.

 



There are various calculators associated with it and each one of those objects in a particular page is created in a separate class file as static methods and they all are accessed through the 'tests' class file in which a static method would be accessing the objects.



Log4j Logging

Log4j is an audit logging framework that gives information about what has happened during execution. It offers the following advantages:

Enables us to understand the application run.

Log output can be saved that can be analyzed later.

Helps in debugging, in case of test automation failures.

Can also be used for auditing purposes to look at the application's health.


Components

1. Instance of Logger class.

2. Log level methods used for logging the messages as one of the following:

error

warn

info

debug

log

Exception Handling

When we are developing tests, we should ensure that the scripts can continue their execution even if the test fails. An unexpected exception would be thrown if the worst case scenarios are not handled properly.

If an exception occurs due to an element not found or if the expected result doesn't match with actuals, we should catch that exception and end the test in a logical way rather than terminating the script abruptly.


Syntax

The actual code should be placed in the try block and the action after exception should be placed in the catch block. Note that the 'finally' block executes regardless of whether the script had thrown an exception or NOT.


try

{

//  Perform  Action

}

catch(ExceptionType1  exp1)

{

//  Catch  block  1

}

catch(ExceptionType2  exp2)

{

//  Catch  block  2

}

catch(ExceptionType3  exp3)

{

//  Catch  block  3

}

finally

{

//  The  finally  block  always  executes.

}


Multi Browser Testing

Users can execute scripts in multiple browsers simultaneously. For demonstration, we will use the same scenario that we had taken for Selenium Grid. In the Selenium Grid example, we had executed the scripts remotely; here we will execute the scripts locally.

 

First of all, ensure that you have appropriate drivers downloaded. Please refer the chapter "Selenium Grid" for downloading IE and Chrome drivers.

Example

For demonstration, we will perform percent calculator in all the browsers simultaneously.


public  class  TestNGClass{

private  WebDriver  driver;

private  String  URL  =  "http://www.calculator.net";


@Parameters("browser") @BeforeTest

public  void  launchapp(String  browser){

if(browser.equalsIgnoreCase("firefox")){

System.out.println("  Executing  on  FireFox");

System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );   

driver  =  new  FirefoxDriver(); 

driver.get(URL);

driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS);

driver.manage().window().maximize();

}

else if (browser.equalsIgnoreCase("chrome")){

System.out.println("  Executing  on  CHROME"); System.out.println("Executing  on  IE"); System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");

driver  =  new  ChromeDriver(); driver.get(URL);

driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS); driver.manage().window().maximize();

}else if (browser.equalsIgnoreCase("ie")){

System.out.println("Executing  on  IE"); System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");

driver  =  new  InternetExplorerDriver(); driver.get(URL);

driver.manage().timeouts().implicitlyWait(10,  TimeUnit.SECONDS); driver.manage().window().maximize();

}else{

throw new IllegalArgumentException("The Browser Type is Undefined");

}

    }



@Test

public  void  calculatepercent(){

//  Click  on  Math  Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();


//  Click  on  Percent  Calculators

driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();


//  Enter  value  10  in  the  first  number  of  the  percent  Calculator driver.findElement(By.id("cpar1")).sendKeys("10");


//  Enter  value  50  in  the  second  number  of  the  percent  Calculator driver.findElement(By.id("cpar2")).sendKeys("50");


//  Click  Calculate  Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/ tr/td[2]/input")).click();


//  Get  the  Result  Text  based  on  its  xpath

 String  result  =

driver.findElement(By.xpath(".//*[@id='content']/p[2]/span

/font/b")).getText();

//  Print  a  Log  In  message  to  the  screen System.out.println("  The  Result  is  "  +  result);

if(result.equals("5")){

System.out.println("  The  Result  is  Pass");

}else{

System.out.println("  The  Result  is  Fail");}

}

@AfterTest

public  void  closeBrowser(){

driver.close();

}

}//class closed


WebElemen

 driver.findElement(By.id("firstname"));

Store WebElement as a variable

WebElement 

Note: Recommendation to use is By class 



WebDriverWait wait = WebDriverWait(driver, 15);









PageObjectModel with page factory

Selenium introduce PageObjectModel including @FindBy and PageFactory.intiElement(driver, Class.class)

  1. Save webElement

@FindBy (how = How.XML, using = “xpath”) WebElement  userName;

  1. To create object of class (instance)and pass driver on that class

LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);


Page Object Model 

https://github.com/myicskt/selenium_pageObjectModel_2023.git

Page Object Model _Using ExcelFile

https://github.com/myicskt/-selenium_pageObjectModel_Excel_2023.git


Note: to get runtime data like(user name generate at run time) s

Page Object Model _Using JOSN (java script object Notaion)

Json is a way of cummintcation





Object vs array

A collection name _vlaue pairs an ordered lis of values

Start  and closing with {} start and closing []

{“name”: “java”, “ver”:”num”} [{“name”: “java”, “ver”:”num”},

{“name”: “php”, “ver”:”num”} }

Json rules 

  1. Json file have .json as extension, ex. User.json

  2. key/value pare {“key”:”value}

  3. Double quotes “ “ ;

  4. Value should be a valid JSON data type

  5. Data is separated by comma (,)

  6. Curly braces should object {....}

  7. Square brackets hold arrays [ ] 


Gson is a Java library → Add Gson dependency 

 




Headless browser 


A headless browser is a web browser without a graphical user interface (GUI). It operates in a "headless" mode, meaning it runs in the background without opening a window or rendering the content visually. Headless browsers are useful for automated testing and web scraping tasks where you don't need to interact with the browser visually but still need to manipulate web pages and extract data.


In Selenium with Java, you can use headless browsers like Chrome or Firefox using their respective headless modes. For example:


Using a headless browser can speed up your tests and make them more efficient because there's no overhead of rendering the web page visually. It also allows you to run tests in environments where there's no GUI available, such as on servers or in CI/CD pipelines.


Chrome 

 You can use Chrome's headless mode by setting the ChromeOptions.addArguments("--headless") .

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

// Create ChromeOptions instance and enable headless mode

ChromeOptions options = new ChromeOptions();

options.addArguments("--headless"); // Enable headless mode

// Instantiate ChromeDriver with ChromeOptions

WebDriver driver = new ChromeDriver(options);





Firefox:

 You can use Firefox's headless mode by using the GeckoDriver and setting the FirefoxOptions FirefoxOptions.setHeadless(true)


// Set the path to the GeckoDriver executable

System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

// Create FirefoxOptions instance and enable headless mode

FirefoxOptions options = new FirefoxOptions();

options.setHeadless(true)

  // Enable headless mode // Instantiate   FirefoxDriver with FirefoxOptions

   WebDriver driver = new FirefoxDriver(options);

// Navigate to a website

driver.get("https://www.example.com");



There are several headless browser. Here is a lis of some commonly used headless browser.

  1. Chrome

  2. Firefox

  3. PhantomJS

  4. HtmlUnit

  5. HtmlUnit

  6. Selenium Grid with Docker: 

Comments


  1. In Selenium's Page Factory, the @FindBy annotation is used to declare a field as a proxy for a WebElement and to specify how to locate that element. There are two ways to use @FindBy:

    Using Locator Strategies directly:

    In this approach, you directly specify the locator strategy (such as id, name, className, xpath, etc.) and the locator value in the @FindBy annotation itself.

    Example:

    java
    Copy code
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;

    public class LoginPage {
    @FindBy(id = "username")
    private WebElement usernameInput;

    @FindBy(id = "password")
    private WebElement passwordInput;

    @FindBy(id = "loginButton")
    private WebElement loginButton;

    // Other elements...
    }
    Using the how and using attributes:

    In this approach, you use the how attribute to specify the locator strategy (enum constant from org.openqa.selenium.support.How) and the using attribute to specify the locator value.

    Example:

    java
    Copy code
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;

    public class LoginPage {
    @FindBy(how = How.ID, using = "username")
    private WebElement usernameInput;

    @FindBy(how = How.ID, using = "password")
    private WebElement passwordInput;

    @FindBy(how = How.ID, using = "loginButton")
    private WebElement loginButton;

    // Other elements...
    }
    Both approaches achieve the same result: they locate the web element using the specified locator strategy and value. The choice between the two approaches is a matter of personal preference and readability. Some developers may prefer the first approach for its simplicity and brevity, while others may prefer the second approach for its explicitness and consistency with the How enum.





    ReplyDelete

Post a Comment

Popular posts from this blog

Important link