You can use also css-selector or Xpath to find the iframe element although this could be resolved by adding ID attribute. This way we won't need to approach development team to add ID attributes.
However, following are some approaches that you can follow to switch frames using protractor.
1st approach: driver.switchTo().frame(driver.findElement(protractor.By.cssselector('css-selector')));
Once you perform the required action on this frame. Please run following code to come out of the iframe: driver.switchTo().defaultContent()
;
2nd approach:
Create a function named SwitchToFrame(By by) and have this code under that function 'driver.switchTo().frame(driver.findElement(protractor.By.cssselector('css-selector')));
'.
In this way, you can call the 'SwitchToFrame(By by)
' function every time you want to switch to the frame by providing the required By argument.
Put this method in root file so that you can access in child pages as well:
function SwitchToFrame(By by) { driver.switchTo().frame(driver.findElement(protractor.By.cssselector('css-selector'))); }
function SwitchToDefaultContent()
{
driver.switchTo().defaultContent();
}
We are using above approaches in our frameworks in automation testing company