PHP-webdriver自動化測試完成登陸

使用facebook- PHP-Webdriver自動化測試 ( 步驟請以github上wiki或者packagist文檔上爲準 )php

 
a、安裝chrome和chrome-driver
 
b、安裝java 並下載selenium的java server -- selenium-server-standalone-3.141.59(獨立服務器),訪問localhost:4444/wd/hub有響應標識成功
 
java -jar selenium-server-standalone-2.39.0.jar
 
c、下載擴展包 - 運行php腳本
 
{
    "require": {
        "facebook/webdriver": "^1.6.0"
    }
}

 

d、獲取cookie用戶登陸憑證java

 

<?php
// 初始化
require_once('./vendor/autoload.php');

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverOptions;

// Selemium服務器
$host = 'http://localhost:4444/wd/hub'; // this is the default
$driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());

// 登陸地址
$driver->get("http://127.0.0.1/index.php");
// 進入iframe
$driver->switchTo()->frame('aa');
// 進入登陸表單iframe
$driver->switchTo()->frame('userLoginWindow_frame');
// 用戶名
$driver->findElement(WebDriverBy::id("ext-comp-1005"))->sendKeys("root");
// 密碼
$driver->findElement(WebDriverBy::id("ext-comp-1008"))->sendKeys("123456");
// 點擊登陸
$driver->findElement(WebDriverBy::id('ext-gen9'))->click();
// 獲取cookie
$cookie = $driver->manage()->getCookies();
print_r($cookie);

 

各個下載地址:git

chrome-driver:github

https://sites.google.com/a/chromium.org/chromedriver/

selenium-server:web

https://www.seleniumhq.org/download/

windows - java:chrome

https://www.java.com/zh_TW/download/help/windows_manual_download.xml

composer - php-web-driver:windows

http://packagist.p2hp.com/packages/facebook/webdriver
相關文章
相關標籤/搜索