Tutorial 9 – Handle frames in Selenide
What you will Learn in this blog:
Handle frames in Selenide
Code snippet
Handle frames in Selenide
A frame is simply an html page embedded within another page.
Go to https://paytm.com/ & click ‘Sign In’.
A separate window opens up and this is frame. Now how do we know that it is a frame?
When we ‘right click’ this window, we can see ‘View frame source’
We will not see ‘View frame source’ option if we click anywhere outside this window
So this was a basic understanding of frame.
Launch the site
https://www.way2automation.com/way2auto_jquery/frames-and-windows.php#load_box
If we right click the middle white portion, we see ‘View frame source”. This means that ‘OPEN NEW WINDOW’ section is a frame
Let us try to click the ‘New Browser Tab’ link
Notice that we get an exception.
The reason being, selenide could not identify the element (since it is inside a frame)
Let us now switch to frame and than perform the same click operation
Notice below that this time the ‘click’ operation is success and the new browser tab gets opened
Similarly, let us now tackle the below scenario
We first ask selenide to click ‘OPEN SEPARATE NEW WINDOW’, than switch to frame and finally click the link
Save and execute
Next
Last scenario
Code snippet
package w2a.selenide;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.switchTo;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
public class FirstSelenideTest {
@Test
public void searchTest() throws InterruptedException {
open("https://www.way2automation.com/way2auto_jquery/frames-and-windows.php#load_box");
/*
//scenario 1:
switchTo().frame($(By.cssSelector(".demo-frame")));
$(By.xpath("//a[contains(text(),'New Browser Tab')]")).click();
Thread.sleep(5000);
*/
/*
//scenario 2:
$(By.xpath("//a[contains(text(),'Open Seprate New Window')]")).click();
switchTo().frame($(By.xpath("//iframe[@src='frames-windows/defult2.html']")));
$(By.linkText("Open New Seprate Window")).click();
Thread.sleep(5000);
*/
/*
//scenario 3:
$(By.xpath("//a[contains(text(),'Frameset')]")).click();
switchTo().frame($(By.xpath("//iframe[@src='frames-windows/defult3.html']")));
$(By.linkText("Open Frameset Window")).click();
Thread.sleep(5000);
*/
//scenario 4:
$(By.xpath("//a[contains(text(),'Open Multiple Windows')]")).click();
switchTo().frame($(By.xpath("//iframe[@src='frames-windows/defult4.html']")));
$(By.linkText("Open multiple pages")).click();
Thread.sleep(10000);
}
}
Thank you for reading!