Skip to content

Commit affb60b

Browse files
authored
Simplified code with assertion support
1 parent 02a08c7 commit affb60b

File tree

1 file changed

+42
-69
lines changed

1 file changed

+42
-69
lines changed

Lambdatest.php

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?php
22

3+
4+
require_once('vendor/autoload.php');
5+
6+
use Facebook\WebDriver\Remote\RemoteWebDriver;
7+
use Facebook\WebDriver\WebDriverBy;
8+
use PHPUnit\Framework\Assert;
9+
10+
311
/*
412
LambdaTest selenium automation sample example
513
Configuration
@@ -12,79 +20,44 @@
1220
Execute PHP Automation Tests on LambdaTest Distributed Selenium Grid
1321
*/
1422

15-
require 'vendor/autoload.php';
16-
17-
class LambdaTest{
18-
19-
/*
20-
Setup remote driver
21-
Params
22-
----------
23-
platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks)
24-
browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge, Safari)
25-
version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/
26-
*/
27-
protected static $driver;
28-
29-
public function searchTextOnGoogle() {
30-
# username: Username can be found at automation dashboard
31-
$LT_USERNAME = "<username>";
23+
# username: Username can be found at automation dashboard
24+
$LT_USERNAME = getenv("LT_USERNAME");
3225

3326
# accessKey: AccessKey can be generated from automation dashboard or profile section
34-
$LT_APPKEY = "<access key>";
35-
36-
$LT_BROWSER = "chrome";
37-
$LT_BROWSER_VERSION ="63.0";
38-
$LT_PLATFORM = "windows 10";
39-
40-
# URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub
41-
$url = "https://". $LT_USERNAME .":" . $LT_APPKEY ."@hub.lambdatest.com/wd/hub";
42-
43-
# setting desired capabilities for the test
44-
45-
try{
46-
$desired_capabilities = new DesiredCapabilities();
47-
$desired_capabilities->setCapability('browserName',$LT_BROWSER);
48-
$desired_capabilities->setCapability('version', $LT_BROWSER_VERSION);
49-
$desired_capabilities->setCapability('platform', $LT_PLATFORM);
50-
$desired_capabilities->setCapability('name', "Php");
51-
$desired_capabilities->setCapability('build', "Php Build");
52-
$desired_capabilities->setCapability('network', false);
53-
$desired_capabilities->setCapability('visual', false);
54-
$desired_capabilities->setCapability('video ', true);
55-
$desired_capabilities->setCapability('console', false);
27+
$LT_ACCESS_KEY = getenv("LT_ACCESS_KEY");
28+
29+
$host= "http://". $LT_USERNAME .":" . $LT_ACCESS_KEY ."@hub.lambdatest.com/wd/hub";
30+
31+
$result="passed";
32+
33+
$capabilities = array(
34+
"build" => "Sample PHP Build",
35+
"name" => "Sample PHP Test",
36+
"platform" => "Windows 10",
37+
"browserName" => "Chrome",
38+
"version" => "88.0"
39+
);
40+
41+
try{
42+
$driver = RemoteWebDriver::create($host, $capabilities);
43+
44+
$driver->get("https://www.google.com/ncr");
5645

57-
/*
58-
Setup remote driver
59-
Params
60-
----------
61-
Execute test: navigate google.com search LambdaTest
62-
Result
63-
-------
64-
print title
65-
*/
66-
self::$driver = RemoteWebDriver::create($url, $desired_capabilities);
67-
68-
self::$driver->get("https://www.google.com/ncr");
69-
70-
$element = self::$driver->findElement(WebDriverBy::name("q"));
71-
if($element) {
46+
$element = $driver->findElement(WebDriverBy::name("q"));
47+
48+
if($element)
49+
{
7250
$element->sendKeys("LambdaTest");
7351
$element->submit();
74-
}
75-
76-
print self::$driver->getTitle();
77-
}catch(Exception $e){
78-
echo "Test failed with reason ".$e->getMessage();
79-
}finally{
80-
// finally quit the driver
81-
self::$driver->quit();
52+
Assert::assertEquals($driver->getTitle(),'LambdaTest - Google Search');
53+
}
54+
} catch(Exception $e) {
55+
$result="failed";
56+
print "Test failed with reason ".$e->getMessage();
57+
}
58+
finally{
59+
// finally quit the driver
60+
$driver->executeScript("lambda-status=".($result == "passed" ? "passed":"failed"));
61+
$driver->quit();
8262
}
83-
}
84-
}
85-
86-
$lambdaTest = new LambdaTest();
87-
$lambdaTest->searchTextOnGoogle();
88-
8963
?>
90-

0 commit comments

Comments
 (0)