In my previous article, IFrame execution, using Selenium Webdriver - Part One, I have explained the usage/advantages of frame and the execution of iframe, using Selenium Webdriver, I have covered one scenario of iframe with Selenium. Before reading this article, I suggest you refer to or read my previous article,
IFrame Execution Using Selenium WebDriver - Part One
In this article, I will cover two different scenarios with an iframe inside another iframe, which are nested iframes.
Scenario 2
From the image given above, we have taken two different frames, which are within an iframe1. We have used another iframe i.e. iframe2. Whenever we want to automate normal controls without any containers like div or an iframe on a Webpage, it will be easy to automate just by using their control properties. Whenever we have controls within an iframe and this iframe is available in another iframe, it will be difficult to identify & automate an iframe and its controls.
In this scenario, we will identify the first frame i.e iframe1 by using its properties like Id="Frame1" or name="Frame1". Once the frame1 is identified, we will switch into the frame1. Subsequently, we will identify each control with the properties like
- For FirstName field of iframe1, we have control properties like id="Fname1" or name="Fname1"
- For LastName field of iframe1, we have control properties like id="Lname1" or name="Lname1"
- For Password field of iframe1, we have control properties like id="Password1" or name="Password1"
- For Email field of iframe1, we have control properties like id="Email1" or name="Email1"
- For Login button of iframe1, we have control properties like id="Login1" or name="Login1"
Once these controls are identified with their control properties, then we can automate each of them, using Selenium. Once all the controls of iframe1 are automated, we will identify the iframe2 with their control properties like id="Frame2" or name="Frame2". Once iframe2 is identified, we will switch into iframe2 and perform automation on the controls, which are available in iframe2.
Example
- For FirstName field of iframe2, we have control properties like id="Fname2" or name="Fname2"
- For LastName field of iframe2, we have control properties like id="Lname2" or name="Lname2"
- For Password field of iframe2, we have control properties like id="Password2" or name="Password2"
- For Email field of iframe2, we have control properties like id="Email2" or name="Email2"
- For Login button of iframe2, we have control properties like id="Login2" or name="Login2"
Once these controls are identified with their control properties, then we can automate each of them, using Selenium Webdriver. Once all the controls of iframe2 are automated, we will switch out of iframe2 and iframe1, so that we can automate the other controls, which are available outside the frames i.e iframe1 & iframe2.
- To automating outer Textbox1 and outer Textbox2, we can identify with its control properties like
- To Outer Textbox1 we have id="outerTxt1" and name="outerTxt1"
Similarly for Outer Textbox2, we have id="outerTxt2" and name="outerTxt2".
HTML for the diagram given above is given below.
- <html>
-
- <head>
- <title>IFRAME EXAMPLE 2</title>
- </head>
-
- <body>
- <iframe src="D:/IFrameExamples/FirstFrame1.html" height="300" id="Frame1" name="Frame1" width="300" scrolling="no" frameborder="0" style=border:solid;color:red " >
- </iframe><br><br>
-
- Outer TextBox1 :<input type="text " id="outerTxt1 " name="outerTxt1 "><br><br>
- Outer TextBox2 :<input type="text " id="outerTxt2 " name="outerTxt2 ">
-
- </body>
- </html>
- <html>
-
- <head>
- <title>FirstFrame1.html</title>
- </head>
-
- <body> First Name : <input type='"text" id="Fname1" name="Fname1"><br><br>
- Last Name : <input type=' "text" id="Lname1" name="Lname1"><br><br> Email : <input type='"text" id="Email1" name="Email1"><br><br>
- Password : <input type=' "text" id="Password1" name="Password1"><br><br> <input type='"text" id="Login1" name="Login1" value="Login"><br><br>
- <iframe src="D:/IFrameExamples/SecondFrame2.Html" id="Frame2" name="Frame2" height ="100" width="100" scrolling="no" frameborder="0" style=border:solid;color:red;>
- </iframe>
-
- inside the iframe2.
- </body>
- </html>
- <html>
-
- <head>
- <title>SecondFrame2.Html</title>
- </head>
-
- <body> First Name : <input type='"text" id="Fname2" name="Fname2"><br><br>
- Last Name : <input type=' "text" id="Lname2" name="Lname2"><br><br> Email : <input type='"text" id="Email2" name="Email2"><br><br>
- Password : <input type=' "text" id="Password2" name="Password2"><br><br> <input type='"text" id="Login2" name="Login2" value="Login"><br><br>
- </body>
- </html>
The code for the execution of IFrame, using Selenium Webdriver is given below.
- Using System;
- Using Mbunit.Framework;
- Uing Microsoft.VisualStudio.TestTools.UnitTesting;
- Using System.Threading;
- Using OpenQA.Selenium;
- Using OpenQA.Selenium.Chrome;
- Namespace SecondScenarioTesting {
- [TestClass]
- Public class SecondScenarioTesting {
- [TestMethod]
- Public void ScenarioSecondTests() {
- Iwebdriver driver = new ChromeDriver("D:/ChromeDriver");
-
- driver.Navigate().GoToUrl("D:/IframeExamples/IFrameExample2");
-
- driver.Manage().Window.Maximize();
- IwebElement FrameElement = driver.FindElement(By.Name("Frame1"));
-
- driver.SwitchTo().Frame(FrameElement);
- IWebElement Fname = driver.FindElement(By.Id("Fname1"));
-
- Fname.SendKeys("Khaja");
- Thread.Sleep(2000);
- IWebElement Lname = driver.FindElement(By.Id("Lname1"));
-
- Lname.SendKeys("Moizuddin");
- Thread.Sleep(2000);
- IWebElement Email = driver.FindElement(By.Name("Email1"));
- Email.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement Password = driver.FindElement(By.Name("Password1"));
- Password.SendKeys("Password12345");
- Thread.Sleep(2000);
- IWebElement LoginClick = driver.FindElement(By.Id("Login1"));
- LoginClick.Click();
- IwebElement SFrameElement = driver.FindElement(By.Name("Frame2"));
- driver.SwitchTo().Frame(SFrameElement);
- IWebElement SFname = driver.FindElement(By.Id("Fname2"));
- 'SFname'
- holds all the information related to FirstName textbox of iframe2.
- SFname.SendKeys("Khaja");
- Thread.Sleep(2000);
- IWebElement SLname = driver.FindElement(By.Id("Lname2"));
- 'SLname'
- holds all the information related to LastName textbox of iframe2.
- SLname.SendKeys("Moizuddin");
- Thread.Sleep(2000);
- IWebElement SEmail = driver.FindElement(By.Name("Email2"));
- SEmail.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement SPassword = driver.FindElement(By.Name("Password2"));
- SPassword.SendKeys("Password12345");
- Thread.Sleep(2000);
- IWebElement SLoginClick = driver.FindElement(By.Id("Login2"));
- SLoginClick.Click();
- driver.SwitchTo().DefaultContent();
- driver.SwitchTo().DefaultContent();
-
- IWebElement outerTextbox1 = driver.FindElement(By.Name("outerTxt1"));
- outerTextbox1.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement outerTextbox2 = driver.FindElement(By.Name("outerTxt2"));
- outerTextbox2.SendKeys("Password12345");
- Thread.Sleep(2000);
Scenario 3
From the scenario given above, within an iframe1, we have two iframes i.e. iframe2 and iframe3 . In order to execute this scenario, first we will find the iframe1 with its control properties like Id="Frame1" or name="Frame1". Once the iframe1 is identified, we will switch into an iframe1 and we will automate the other controls available inside the iframe1 like
- For FirstName field of iframe1, we have control properties like id="Fname1" or name="Fname1".
- For LastName field of iframe1, we have control properties like id="Lname1" or name="Lname1".
- For Password field of iframe1, we have control properties like id="Password1" or name="Password1".
- For Email field of iframe1, we have control properties like id="Email1" or name="Email1".
- For Login button of iframe1, we have control properties like id="Login1" or name="Login1".
Once all the controls are identified and automated then we will identify the iframe 2 with its control properties like Id="Frame2" or name="Frame2".
After finding the iframe2 we will switch into the iframe2 and identify each and every control with their properties like.
- For FirstName field of iframe2, we have control properties like id="Fname2" or name="Fname2".
- For LastName field of iframe2, we have control properties like id="Lname2" or name="Lname2".
- For Password field of iframe2, we have control properties like id="Password2" or name="Password2".
- For Email field of iframe2, we have control properties like id="Email2" or name="Email2".
- For Login button of iframe2, we have control properties like id="Login2" or name="Login2".
Once all the controls are identified and automated within the iframe2, we will switch out of iframe2, then we will identify the iframe 3 with its control properties like Id="Frame3" or name="Frame3". Once iframe3 is identified then we will switch in to iframe3 by identifying its controls with their control properties like.
- For FirstName field of iframe3, we have control properties like id="Fname3" or name="Fname3"
- For LastName field of iframe3, we have control properties like id="Lname3" or name="Lname3"
- For Password field of iframe3, we have control properties like id="Password3" or name="Password3"
- For Email field of iframe3 we, have control properties like id="Email3" or name="Email3"
- For Login button of iframe3, we have control properties like id="Login3" or name="Login3".
Once all the controls of iframe3 are automated, we will switch out of iframe3 and then iframe1.
HTML from the diagram given above is given below.
- <html>
-
- <head>
- <title>IFRAME EXAMPLE 3</title>
- </head>
-
- <body>
- <iframe src="D:/IFrameExamples/FirstFrame1.html" height="400" width="400" id="Frame1" name="Frame1" scrolling="no" frameborder="0" style=border:solid; color:red; ">
- <iframe>
-
- </body>
- </html>
- <html>
-
- <head>
- <title>FirstFrame1.html</title>
- </head>
-
- <body> FirstName :<input type="text" id="Fname1" Name="Fname1"><br><br> LastName :<input type="text" id="Lname1" Name="Lname1"><br><br> Email :<input type="text" id="Email1" Name="Email1"><br><br> Password :<input type="password" id="Password1" Name="Password1"><br><br> <input type="button" value="Login" id="Login1" name="Login1"> <iframe src="D:/IFrameExamples/SecondFrame2.html" height="80" id="Frame2" name="Frame2" width="80" scrolling="no" frameborder="0" style=border:solid; color:red; ">
- </iframe>
-
- iframe2.
- <iframe src="D:/IFrameExamples/ThirdFrame3.html " height="80 " id="Frame3 " name="Frame3 " width="80 " scrolling="no " frameborder="0 " style=border:solid; color:red;"> </iframe>
-
- </html>
- <html>
-
- <head>
- <title> SecondFrame2.html </title>
- </head>
-
- <body> FirstName :<input type="text" id="Fname2" Name="Fname2"><br><br> LastName :<input type="text" id="Lname2" Name="Lname2"><br><br> Email :<input type="text" id="Email2" Name="Email2"><br><br> Password :<input type="password" id="Password2" Name="Password2"><br><br> <input type="button" value="Login" id="Login2" name="Login2"> </body>
-
- </html>
ThirdFrame3.html
- <html>
-
- <head>
- <title> ThirdFrame3.html </title>
- </head>
-
- <body> FirstName :<input type="text" id="Fname3" Name="Fname3"><br><br> LastName :<input type="text" id="Lname3" Name="Lname3"><br><br> Email :<input type="text" id="Email3" Name="Email3"><br><br> Password :<input type="password" id="Password3" Name="Password3"><br><br> <input type="button" value="Login" id="Login3" name="Login3"> </body>
-
- </html>
The code for the execution of IFrame is given below, using Selenium Webdriver.
- Using System;
- Using Mbunit.Framework;
- Uing Microsoft.VisualStudio.TestTools.UnitTesting;
- Using System.Threading;
- Using OpenQA.Selenium;
- Using OpenQA.Selenium.Chrome;
- Namespace ThirdScenarioTesting {
- [TestClass]
- Public class ThirdScenarioTesting {
- [TestMethod]
- Public void ScenarioThirdTests() {
- Iwebdriver driver = new ChromeDriver("D:/ChromeDriver");
- driver.Navigate().GoToUrl("D:/IframeExamples/IFrameExample3");
- driver.Manage().Window.Maximize();
- IwebElement FrameElement = driver.FindElement(By.Name("Frame1"));
- driver.SwitchTo().Frame(FrameElement);
- IWebElement Fname = driver.FindElement(By.Id("Fname1"));
- Fname.SendKeys("Khaja");
- / Used to enter the given value to textbox field.
- Thread.Sleep(2000);
- IWebElement Lname = driver.FindElement(By.Id("Lname1"));
- Lname.SendKeys("Moizuddin");
- Thread.Sleep(2000);
- IWebElement Email = driver.FindElement(By.Id("Email1"));
- Email.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement Password = driver.FindElement(By.Id("Password1"));
- Password.SendKeys("Password12345");
- Thread.Sleep(2000);
- IWebElement Loginclick = driver.FindElement(By.Id("Login1"));
- Loginclick.Click();
- Thread.Sleep(2000);
- IwebElement FrameElement2 = driver.FindElement(By.Name("Frame2"));
- driver.SwitchTo().Frame(FrameElement2);
- IWebElement SFname = driver.FindElement(By.Id("Fname2"));
- SFname.SendKeys("Khaja");
- / Used to enter the given value to textbox field.
- Thread.Sleep(2000);
- IWebElement SLname = driver.FindElement(By.Id("Lname2"));
- SLname.SendKeys("Moizuddin");
- Thread.Sleep(2000);
- IWebElement SEmail = driver.FindElement(By.Id("Email2"));
- SEmail.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement SPassword = driver.FindElement(By.Id("Password2"));
- SPassword.SendKeys("Password12345");
- Thread.Sleep(2000);
- IWebElement SLoginclick = driver.FindElement(By.Id("Login1"));
- SLoginclick.Click();
- Thread.Sleep(2000);
- driver.SwitchTo().DefaultContent();
- IwebElement FrameElement3 = driver.FindElement(By.Name("Frame3"));
- driver.SwitchTo().Frame(FrameElement3);
- IWebElement TFname = driver.FindElement(By.Id("Fname3"));
- TFname.SendKeys("Khaja");
- Thread.Sleep(2000);
- IWebElement TLname = driver.FindElement(By.Id("Lname3"));
- TLname.SendKeys("Moizuddin");
- Thread.Sleep(2000);
- IWebElement TEmail = driver.FindElement(By.Id("Email3"));
- TEmail.SendKeys("[email protected]");
- Thread.Sleep(2000);
- IWebElement TPassword = driver.FindElement(By.Id("Password3"));
- TPassword.SendKeys("Password12345");
- Thread.Sleep(2000);
- IWebElement TLoginclick = driver.FindElement(By.Id("Login3"));
- TLoginclick.Click();
- Thread.Sleep(2000);
- driver.SwitchTo().DefaultContent();
- driver.SwitchTo().DefaultContent();
- driver.Quit();
- }
- }
- }
Please provide me the feedback for my article. If I missed anything, please let me know in the comment given below.
I hope, it helps.