Created
December 15, 2021 16:00
-
-
Save muditlambda/7324c189da68ce413b5d4439cb1fb1f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MyTests; | |
| import org.openqa.Selenium.By; | |
| import org.openqa.Selenium.WebElement; | |
| import org.openqa.Selenium.remote.DesiredCapabilities; | |
| import org.openqa.Selenium.remote.RemoteWebDriver; | |
| import org.testng.annotations.AfterTest; | |
| import org.testng.annotations.BeforeTest; | |
| import org.testng.annotations.Parameters; | |
| import org.testng.annotations.Test; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.concurrent.TimeUnit; | |
| public class Demo { | |
| String username = "user-name"; //Enter your username | |
| String accesskey = "access-key"; //Enter your accesskey | |
| static RemoteWebDriver driver; | |
| String gridURL = "@hub.lambdatest.com/wd/hub"; | |
| String urlToTest = "https://www.rediff.com/"; | |
| @BeforeTest | |
| @Parameters("browser") | |
| public void setup(String browser) { | |
| System.out.println("Setting up the drivers and browsers"); | |
| DesiredCapabilities capabilities = new DesiredCapabilities(); | |
| if(browser.equals("Chrome")) { | |
| capabilities.setCapability("platform", "Windows 10");// To specify the OS | |
| capabilities.setCapability("browserName", "Chrome"); //To specify the browser | |
| capabilities.setCapability("version","94.0"); //To specify the browser | |
| capabilities.setCapability("build", "ChromeTests"); //To identify the test | |
| capabilities.setCapability("name", "Handle_iframes"); | |
| } | |
| else if (browser.equals("Firefox")){ | |
| capabilities.setCapability("browserName", "Firefox"); //To specify the browser | |
| capabilities.setCapability("version", "93.0"); //To specify the browser version | |
| capabilities.setCapability("platform", "Windows 10"); // To specify the OS | |
| capabilities.setCapability("build", "FirefoxTests"); //To identify the test | |
| capabilities.setCapability("name", "Handle_iframes"); | |
| } | |
| else if (browser.equals("Edge")){ | |
| capabilities.setCapability("browserName", "MicrosoftEdge"); | |
| capabilities.setCapability("platform", "Windows 10"); | |
| capabilities.setCapability("version","94.0"); // To specify the OS | |
| capabilities.setCapability("build", "EdgeTests"); //To identify the test | |
| capabilities.setCapability("name", "Handle_iframes"); | |
| } | |
| capabilities.setCapability("network", true); // To enable network logs | |
| capabilities.setCapability("visual", true); // To enable step by step screenshot | |
| capabilities.setCapability("video", true); // To enable video recording | |
| capabilities.setCapability("console", true); // To capture console logs | |
| try { | |
| driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); | |
| } catch (MalformedURLException e) { | |
| System.out.println("Invalid grid URL"); | |
| } catch (Exception e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| } | |
| @Test | |
| public void test1_switchUsingId(){ | |
| System.out.println("Switching iFrame using id has started"); | |
| driver.get(urlToTest); | |
| driver.manage().window().maximize(); | |
| driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); | |
| driver.switchTo().frame("moneyiframe"); //moneyiframe is the id of the iframe | |
| WebElement option = driver.findElement(By.id("nseindex")); | |
| String nse_Value = option.getText(); | |
| System.out.println("The NSE value is " +nse_Value); | |
| System.out.println("Switching iFrame using id has ended"); | |
| } | |
| @Test | |
| public void test2_switchUsingName(){ | |
| System.out.println("Switching iFrame using name has started"); | |
| driver.get(urlToTest); | |
| driver.manage().window().maximize(); | |
| driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); | |
| driver.switchTo().frame("moneyiframe"); //moneyiframe is the name of the iframe | |
| WebElement option = driver.findElement(By.id("nseindex")); | |
| String nse_Value = option.getText(); | |
| System.out.println("The NSE value is " +nse_Value); | |
| System.out.println("Switching iFrame using name has ended"); | |
| } | |
| @Test | |
| public void test3_switchUsingIndex(){ | |
| System.out.println("Switching iFrame using index has started"); | |
| driver.get(urlToTest); | |
| driver.manage().window().maximize(); | |
| driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); | |
| driver.switchTo().frame(0); //switching to 1st frame | |
| WebElement option = driver.findElement(By.id("nseindex")); | |
| String nse_Value = option.getText(); | |
| System.out.println("The NSE value is " +nse_Value); | |
| System.out.println("Switching iFrame using index has ended"); | |
| } | |
| @Test | |
| public void test1_switchUsingWebElement(){ | |
| System.out.println("Switching iFrame using a webElement has started"); | |
| driver.get(urlToTest); | |
| driver.manage().window().maximize(); | |
| driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); | |
| WebElement iframe_element = driver.findElement(By.xpath("//iframe[@class='moneyiframe']")); | |
| driver.switchTo().frame(iframe_element); | |
| WebElement option = driver.findElement(By.id("nseindex")); | |
| String nse_Value = option.getText(); | |
| System.out.println("The NSE value is " +nse_Value); | |
| System.out.println("Switching iFrame using a webElement has ended"); | |
| } | |
| @AfterTest | |
| public void tearDown(){ | |
| driver.close(); | |
| System.out.println("Tests ended successfully"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment