Miscellaneous Selenium Scenarios



  • To handle alert window in Selenium Webdriver we have predefined Interface known as Alert.

  • Alert Interface has some methods-

  • There are the four methods that we would be using along with the Alert interface:

    1. void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.

    2. void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.

    3. String getText() – The getText() method returns the text displayed on the alert box.

    4. void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.

  • Note - Since alert is separate window so before using these methods we have to switch to alert window using "switchTo()" method.

Syntax for switching a frame: driver.switchTo().frame(WebElement frameElement);

  • Description: Select a frame using its previously located WebElement.

  • Parameters: frameElement - The frame element to switch to.

  • Returns: Driver focused on the given frame (current frame).

  • Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element.

  • StaleElementReferenceException - If the WebElement has gone stale.

Below is an example with a code which sends an Element to the switch.


Some times when there are multiple frames (i.e. frame inside a frame), we need to first switch to the parent frame and then we move to the child frame. Below is the code snippet to work with multiple frames.


After working with the frames, main important is to come back to the web page. if we don’t switch back to the default page, driver will throw an exception. Below is the code snippet to switch back to the default content.



Usage of Select Class for Handling Dropdown Elements:

Select is a class which is provided by Selenium to perform multiple operations on DropDown object and Multiple Select object. This class can be found under the Selenium’s Support.UI. Select package.

The below is the sample html code of Dropdown.


Webdriver code for Selecting a Value using select.selectByValue(Value):


SelectByIndex:

It is almost the same as “selectByValue” but the only difference is that we provide the ‘index number of the option’ here rather than ‘option text’. It takes a parameter of int which is the index value of “Select element” and it returns nothing.

Syntax:


SelectByVisibleText:

Syntax:


  • A HTTP cookie comprises of information about the user and their preferences.

  • It stores information using a key-value pair. It is a small piece of data sent from Web Application and stored in Web Browser even while the user is browsing that website.

Why Handle Cookies in Selenium?

  • Each cookie is associated with a name, value, domain, path, expiry and the status of the data whether it is secured or not. In order to validate a client, a server parses all of these values in a cookie. When testing a web application using selenium web driver, you may need to create, update or delete a cookie.

  • For example, when automating Online Shopping Application, you many need to automate test scenarios like place order, view cart, payment information, order confirmation, etc.

  • If cookies are not stored then you need to perform login action every time before you execute the above listed test scenarios. This will increase your coding effort and execution time.

  • The solution is to store cookies in a file. Later on you can retrieve the values of cookie from this file and add it to your current browser session. As a result, you can skip the login steps in every test case because your driver session will have this information in it.

  • The application server now treats your browser session as authenticated one and directly takes you to your requested URL.

In Selenium Webdriver, we can query and interact with cookies with below given built-in method:


Try printing all cookies in our example from flipkart.com.


What is Untrusted SSL certificate?

  • Whenever we try to access HTTPS website or application so many time you will face untrusted SSL certificate issue. This issue comes in all browser like IE,Chrome,Safari, Firefox etc.

untrusted SSL certificate issue

Why we get this certificate issues often?

  • Each secure site has Certificate so its certificate is not valid up-to-date.

  • Certificate has been expired on date.

  • Certificate is only valid for (site name).

  • The certificate is not trusted because the issuer certificate is unknown due to many reasons.

Handle Untrusted Certificate Selenium

  • Step 1- We have to create FirefoxProfile in Selenium.

  • Step 2- We have some predefined method in Selenium called setAcceptUntrustedCertificates() which accept Boolean values (true/false)- so we will make it true.

  • Step 3- Open Firefox browser with the above-created profile.


Handle Untrusted Certificate in Firefox


Handle Untrusted Certificate in Chrome


Handle Untrusted Certificate in IE


There are many advantages by using FirefoxProfile preferences in selenium. You have to update the preferences within Firefox. We can do this by instantiating a Firefox Profile object and then update the settings.We will then need to pass this object into FirefoxDriver which will load the profile with your defined settings.

How to setup the startup page in Firefox browser?


How to set language in profile?


How to Change the user agent?


How to download the files using firefox profile?


How to handle SSL certificate error (Firefox browser)?