2

我有一些 PHP 代码使用图形 api 在我的应用程序中创建 Facebook 测试帐户。我已经测试了这段代码,它工作正常。

我在使用Selenium Webdriver (for PHP)作为测试用户实际登录 Facebook 时遇到了麻烦。

这是我的代码的简化版本:

class FB_OAuthTest extends PHPUnit_Framework_TestCase {
    private $fbUtils = null;
    private $fbUser = null;
    private $startUrl = 'http://my.start.page';

    protected function setUp() {
        $this->webdriver = new WebDriver(Config::SELENIUM_SERVER, Config::SELENIUM_PORT);
        $this->webdriver->connect(Config::BROWSER);

        $this->fbUtils = new FBUtils(Config::FB_APP_ID, Config::FB_APP_SECRET);

        // create new FB test user here
        $this->fbUser = $this->fbUtils->createNewTestUser();
    }

    protected function tearDown() {
        // delete the test user when we're done
        if($this->fbUser != null) {
            $this->fbUtils->deleteTestUser($this->fbUser->id);
        }
        $this->webdriver->close();
    }

    // the magic happens here
    public function testOAuth() {    
        // "login" as test user by requesting the login url that they give
        $this->webdriver->get($this->fbUser->login_url);

        // start the app workflow: go to a page with a FB OAuth button
        $this->webdriver->get($this->startUrl); 
        $this->assertTrue(isTextPresent("FB_OAuth_Test_Start", $this->webdriver));

        // click the button to begin the FB OAuth process
        $oAuthButton = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//button[@control='FBLogin1']");
        $oAuthButton->click();

        // The Facebook login/OAuth dialog shows up now, and forces me to login
        // despite first going to the FB user's login url

        // sleep for the hell of it, just to make sure the FB login dialog loads
        sleep(5);

        // Selenium fails here - not finding the input with id='email', despite it existing on the page
        $emailField = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[id='email']");
        if ($emailField) {
            $emailField->sendKeys(array($this->fbUser->email));
            $emailField->submit();
        } else {
            $this->fail('FB login email field not found');
        }

        $passwordField = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[id='pass']");
        if ($passwordField) {
            $passwordField->sendKeys(array($this->fbUser->password));
            $passwordField->submit();
        } else {
            $this->fail('FB login password field not found');
        }

        $loginButton = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[name='login']");
        if ($loginButton) {
            $loginButton->click();
        } else {
            $this->fail('FB login button not found');
        }

        $grantAppPermission = $this->webdriver->findElementBy(LocatorStrategy::name, "grant_clicked");
        $grantAppPermission->click();

        $this->assertTrue(isTextPresent("FB_OAuth_Test_Success", $this->webdriver));
    }
}

从代码注释中可以看出,Selenium 找不到 'email' 输入元素,测试失败。任何想法将不胜感激。谢谢!

更新

我什至尝试过做一些相当直接的事情,比如代码崩溃,但它仍然不起作用。

private function loginToFacebook() {
    $this->webdriver->get('https://www.facebook.com/login.php?login_attempt=1');

    sleep(1);

    $emailField = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[id='email']");
    if ($emailField) {
        $emailField->sendKeys(array($this->fbUser->email));
        $emailField->submit();
    } else {
        $this->fail('FB login email field not found');
    }

    $passwordField = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[id='pass']");
    if ($passwordField) {
        $passwordField->sendKeys(array($this->fbUser->password));
        $passwordField->submit();
    } else {
        $this->fail('FB login password field not found');
    }

    $loginButton = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[name='login']");
    if ($loginButton) {
        $loginButton->click();
    } else {
        $this->fail('FB login button not found');
    }
}

更新:这是工作代码

private function loginToFacebook() {
    $this->webdriver->get('https://www.facebook.com/login.php?login_attempt=1');

    $emailField = $this->webdriver->findElementBy(LocatorStrategy::id, 'email');
    if ($emailField) {
        $emailField->sendKeys(array($this->fbUser->email));
    } else {
        $this->fail('FB login email field not found');
    }

    $passwordField = $this->webdriver->findElementBy(LocatorStrategy::id, 'pass');
    if ($passwordField) {
        $passwordField->sendKeys(array($this->fbUser->password));
    } else {
        $this->fail('FB login password field not found');
    }

    $loginButton = $this->webdriver->findElementBy(LocatorStrategy::xpath, "//input[@name='login']");
    if ($loginButton) {
        $loginButton->click();
    } else {
        $this->fail('FB login button not found');
    }
}
4

1 回答 1

2

您错误地定义了您的 xpath 表达式

应该是//input[@id='email'] ##Note the @ sign (not sure you need to escape '@' with a blackslash)

但仍然尝试改变您尝试定位 Web 元素的方式

 // try locating by LocatorStrategy id
        $emailField = $this->webdriver->findElementBy(LocatorStrategy::id, 'email');
        if ($emailField) {
            $emailField->sendKeys(array($this->fbUser->email));
            $emailField->submit();
        } else {
            $this->fail('FB login email field not found');
        }

与 相比,id它的搜索速度更快。namexpath

对密码字段也进行相同的更改,就像这样

$passwordField = $this->webdriver->findElementBy(LocatorStrategy::id, "pass");

于 2012-12-20T05:48:32.853 回答