Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active September 7, 2021 02:33
Show Gist options
  • Save nadvolod/7cdafa4d4fd545d969db99621e93aa3e to your computer and use it in GitHub Desktop.
Save nadvolod/7cdafa4d4fd545d969db99621e93aa3e to your computer and use it in GitHub Desktop.
How to work with Selenium Webdriver iFrames
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumWebdriverTips
{
[TestClass]
public class RandomSeleniumTricks
{
private static LanguageType GetLanguageInUse(string content,
LanguageType expectedLanguage = LanguageType.NoLanguageDetermined)
{
content = content.Trim();
var language = DetermineLanguage(content);
if (language != expectedLanguage && expectedLanguage != LanguageType.NoLanguageDetermined)
language = OverrideUnexpectedLanguage(content, expectedLanguage);
return language;
}
IWebDriver _driver = new FirefoxDriver();
WebDriverWait _wait;
[TestMethod]
public void IFrames()
{
//create an instance of the WebDriverWait class so that we can dynamically wait for frames
// Video Tutorial Here - http://courses.ultimateqa.com/courses/synchronization-techniques
_wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(6));
//navigate Dave Haeffner's practice automation page
_driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/tinymce");
//wait until a frame is present and then switch to it
_wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Id("mce_0_ifr")));
//find the editor and get the text
var editor = _driver.FindElement(By.Id("tinymce"));
var startText = editor.Text;
//clear the editor, type a string, get the text
editor.Clear();
editor.SendKeys("www.ultimateqa.com");
var endText = editor.Text;
//make sure that we actually typed in the text in the appropriate place
Assert.AreNotEqual(endText, startText);
//Don't forget to switch to the default content when you're done messing with the page
_driver.SwitchTo().DefaultContent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment