public function tearDown() { // return if the driver wasn't initialized if (!$this->_driver) { return; } if (self::$_config['persist']) { try { $alert = new Alert($this->_driver); $alert->dismiss(); } catch (Exception $e) { } try { foreach ($this->_driver->getWindowHandles() as $handle) { // skip the original window if ($handle == self::$_handle) { continue; } // try to close any other windows that were opened try { $this->_driver->switchTo()->window($handle); $this->_driver->close(); } catch (Exception $e) { } } $this->_driver->switchTo()->window(self::$_handle); $this->_driver->switchTo()->activeElement(); } catch (SeleniumUnknownErrorException $e) { // test case may have closed the parent window self::$_handle = null; return; } $this->_driver->manage()->deleteAllCookies(); $this->_driver->manage()->timeouts()->implicitWait(0); $this->_driver->manage()->window()->setPosition($this->_position['x'], $this->_position['y']); $this->_driver->manage()->window()->setSize($this->_size['width'], $this->_size['height']); $this->_driver->manage()->timeouts()->pageLoadTimeout(10000); } else { try { $this->_driver->quit(); } catch (Exception $e) { } } }
public function testDemo1() { //get url $this->_driver->get($this->_testUrl); sleep(4); //access text input $webElement = $this->_driver->findElement(By::id("txt1")); $webElement->clear(); $webElement->sendKeys("Text sent 1"); $this->assertEquals("Text sent 1", $webElement->getAttribute("value")); $webElement = $this->_driver->findElement(By::id("txt2")); $webElement->clear(); $webElement->sendKeys("Text sent 2"); $this->assertEquals("Text sent 2", $webElement->getAttribute("value")); //access listbox $selectElement = new SelectElement($this->_driver->findElement(By::id("sel1"))); $selectElement->selectByValue("4"); $this->assertTrue($selectElement->getElement()->findElement(By::xPath(".//option[@value = 4]"))->isSelected()); //access checkbox $webElement = $this->_driver->findElement(By::cssSelector("html body table tbody tr td fieldset form p input#chk3")); $webElement->click(); $this->assertTrue($webElement->isSelected()); //access radio $webElement = $this->_driver->findElement(By::id("rd3")); $webElement->click(); $this->assertTrue($webElement->isSelected()); //access button $webElement = $this->_driver->findElement(By::id("btnSubmit")); $webElement->click(); //access h2 $webElement = $this->_driver->findElement(By::cssSelector("html body h2#h2FormReceptor")); $this->assertEquals("Form receptor", $webElement->getText()); //navigation $this->_driver->get('http://www.nearsoft.com'); $this->_driver->navigate()->refresh(); $this->_driver->navigate()->back(); $this->_driver->navigate()->forward(); sleep(5); }
public function testDemo2() { //click button/get alert text $this->_driver->get($this->_testUrl); $this->_driver->findElement(By::id("btnAlert"))->click(); $alert = $this->_driver->switchTo()->alert(); $this->assertEquals("Here is the alert", $alert->getText()); $alert->accept(); //get main window handle $mainWindowHandle = $this->_driver->getWindowHandle(); //open popup window / handle its elements $this->_driver->findElement(By::id("btnPopUp1"))->click(); $this->_driver->switchTo()->window("popup1"); $webElement = $this->_driver->waitForElementUntilIsPresent(By::id("txt1")); $webElement->sendKeys("test window"); $this->assertEquals("test window", $webElement->getAttribute("value")); $this->_driver->close(); $this->_driver->switchTo()->window($mainWindowHandle); //get iframe / handle its elements $this->_driver->switchTo()->frame("iframe1"); $webElement = $this->_driver->waitForElementUntilIsPresent(By::id("txt1")); $webElement->sendKeys("test iframe"); $this->assertEquals("test iframe", $webElement->getAttribute("value")); $this->_driver->switchTo()->window($mainWindowHandle); //wait for element to be present $this->_driver->findElement(By::id("btnAppendDiv"))->click(); $wait = new WebDriverWait(8); $label = $wait->until($this->_driver, "findElement", array(By::id("dDiv1-0"), true)); $this->assertEquals("Some content", $label->getText()); sleep(5); }