如何在selenium PHP单元中切换到iframe

时间:2014-12-13 07:58:35

标签: php selenium iframe phpunit

我想编写测试来登录网站。问题是当我点击登录时出现一个iframe的弹出窗口。现在我编写代码在窗口之间切换。但我切换到不同的iframe。我不知道它来自哪里这里是一段代码。我使用Facebook编写的selenium WebDriver。

<?php
class TitleTest extends PHPUnit_Framework_TestCase {

protected $webDriver;
public function setUp()
{
    $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
    $this->webDriver = RemoteWebDriver::create('http://ip-address:4444/wd/hub', $capabilities);
}
protected $url = 'https://mywebsite.net';

public function testAfterLoginBudliTitle()
{
    // Sell used Login and account page's title assert
    $this->webDriver->get($this->url);
    $this->webDriver->manage()->window()->maximize();
    $link = $this->webDriver->findElement(WebDriverBy::xpath('/html/body/div[3]/header/div[2]/div/ul/li[2]/a'));
    sleep(2);        
    $link->click(); //click on login
    sleep(3);
    $iframe = $this->webDriver->findElement(WebDriverBy::tagName('iframe'));

    $frameName = $iframe->getAttribute('name');
    //Switch the driver to the iframe
    $this->webDriver->switchTo()->frame($frameName);
    $this->assertContains('demo', $frameName); // Here I get the different iframe name
 }  
}
?>

1 个答案:

答案 0 :(得分:2)

为了与框架或iframe内的元素进行交互,请先切换到该框架!

$my_frame = 'id_or_name';
$driver->switchTo()->frame($my_frame);

... do your stuff here and switch back afterwards ...

$driver->switchTo()->defaultContent();

查看维基。 https://github.com/facebook/php-webdriver/wiki/Alert,-Window-Tab,-frame-iframe-and-active-element#frame-iframe