HomeCodeceptJS

CodeceptJS – Switching to Frames

CodeceptJS – Switching to Frames

<<Previous Post               <<Complete CodeceptJS Tutorial>>               Next Post>>

CodeceptJS – Switching To Frames

switchTo

Switches frame or in case of null locator reverts to parent.

What is an Iframe?

Iframe extends to the Inline frame. Iframe in simple terms is a website inside another website.

It can be found as <iframe src=”https//book.theautomatedtester.co.uk”></iframe> in Document object model.

Pre-requisite: Have ‘Visual Studio Code’ IDE installed in your machine (Click here to install if not installed in your machine)

Here is the small Exercise to Locate Iframe Element using SwitchTo command of CodeceptJS and then enter text inside the text box present in the selected iframe.

Exercise:-

1. Navigate to http://omayo.blogspot.com/

2. Find Iframe you want to locate inside http://omayo.blogspot.com webpage.

3. Find Textbox inside Iframe and Enter Text into it.

Practical Demonstration:

Following are the Steps to Perform the above Exercise (i.e. Switching to Iframes) using CodeceptJS Automation code in ‘Visual Studio Code’ IDE:

Step 1:- Create a Folder named as CodeceptJS_Switching_To_Iframes(You can create a folder with any name)

Open the Git Bash from the above-created folder (In our case ‘CodeceptJS_Switching_To_Iframes’ folder)

Trigger the below command in Terminal :

npm init -y

The above command will create a default package.json file in the ‘CodeceptJS_Switching_To_Iframes’ folder.

Attached Screenshot for Reference:-

Step 2:- Install codeceptjs with webdriverio as a helper. 

Run the below command in Terminal:

npm install codeceptjs webdriverio --save-dev

Step 3:- Generate Configuration File. Since we haven’t created one yet, codeceptJS is smart enough to figure that out and help us through the process.

Run the below command in Terminal:

 npx codeceptjs init

Here are the answers I chose: (Move your arrow buttons on the keyboard to select the options and enter)

  • Where are your tests located ./*_test.js
  • What helpers do you want to use? WebDriver
  • Where should logs, screenshots, and reports to be stored? ./output
  • Do you want localization for tests? (See https://codecept.io/translation/) English (no localization)
  • [WebDriver] Base URL of the site to be tested http://www.omayo.blogspot.com
  • [WebDriver] Browser in which testing will be performed chrome

These were the main points i have selected rest you can go through the screenshot given below:-

Verify codecept.conf.js file gets created successfully.

Step 4:- Now Install Selenium standalone service

Run the below command in Terminal:

npm install @wdio/selenium-standalone-service --save-dev

Step 5:- As generated config file doesn’t auto-configure selenium-standalone in its codecept.conf.js file. Open the file in the respective IDE you use, we are using Visual Studio Code as IDE.

wdio: {
     enabled: true,
     services: [“selenium-standalone”]
   }

Step 6:- Write code in the test file. I have written a sample code through which switching to iframe2 will take place and some text will be entered in the text box inside the iframe2 (PS: can be done in multiple ways but i am presenting one of the ways). Use this code and paste it in your test file created during the generation of the config file in Step 3

Feature('switch to Iframe2 and put some text inside the textbox');
Scenario('User should be able to switch to Iframe2 and write some text inside the textbox', ({ I }) => {
   I.amOnPage("/");
   I.scrollTo("div[id='HTML31'] h2");
   I.switchTo("#iframe2");
   I.fillField("#q",'sample text');
   I.wait(3);
});


Step 7:- Run your test file to perform the test.

Run the below command in Visual Studio Code Terminal:

npx codeceptjs run

Download the Project from here (Click here to Download) and Unzip it.

Hopefully, your test completed and passed.
Comment ‘Success’ in this post if you are able to practically achieve this exercise.

Next Steps:

> To learn more about CodeceptJS, continue to the next post (Click on Next Post link below)

> Check complete CodeceptJS Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning ?

About Me > Ajay Lunia 

Connect to me on LinkedIn (Click here)

On a mission to help the Software Testing Community in all possible ways.

<<Previous Post               <<Complete CodeceptJS Tutorial>>               Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp