-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBasicAuthentication.java
More file actions
148 lines (121 loc) · 5.11 KB
/
BasicAuthentication.java
File metadata and controls
148 lines (121 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.lambdatest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.HasAuthentication;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class BasicAuthentication {
public static String hubURL = "https://hub.lambdatest.com/wd/hub";
private WebDriver driver;
public void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", "127");
Map<String, Object> ltOptions = new HashMap<>();
ltOptions.put("user", System.getenv("LT_USERNAME"));
ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY"));
ltOptions.put("build", "Selenium 4");
ltOptions.put("name", this.getClass().getName());
ltOptions.put("platformName", "Windows 10");
ltOptions.put("seCdp", true);
ltOptions.put("selenium_version", "4.23.0");
capabilities.setCapability("LT:Options", ltOptions);
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
System.out.println(driver);
}
public void authentication() {
Augmenter augmenter = new Augmenter();
driver = augmenter.augment(driver);
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
driver = augmenter.addDriverAugmentation("chrome", HasAuthentication.class,
(caps, exec) -> (whenThisMatches, useTheseCredentials) -> devTools.getDomains().network()
.addAuthHandler(whenThisMatches, useTheseCredentials))
.augment(driver);
((HasAuthentication) driver).register(UsernameAndPassword.of("foo", "bar"));
driver.get("http://httpbin.org/basic-auth/foo/bar");
String text = driver.findElement(By.tagName("body")).getText();
System.out.println(text);
if (text.contains("authenticated")) {
markStatus("passed", "Authentication Successful", driver);
} else {
markStatus("failed", "Authentication Failure", driver);
}
}
public void tearDown() {
try {
driver.quit();
} catch (
Exception e) {
markStatus("failed", "Got exception!", driver);
e.printStackTrace();
driver.quit();
}
}
public static void markStatus(String status, String reason, WebDriver driver) {
JavascriptExecutor jsExecute = (JavascriptExecutor) driver;
jsExecute.executeScript("lambda-status=" + status);
System.out.println(reason);
}
public class SimpleFormDemoTest {
WebDriver driver;
@BeforeClass
public void setUp() {
// Set up ChromeDriver path if not in system PATH
// System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.lambdatest.com/selenium-playground");
}
@Test
public void testSimpleFormDemo() {
// 2. Click "Simple Form Demo"
driver.findElement(By.linkText("Simple Form Demo")).click();
// 3. Validate that the URL contains "simple-form-demo"
String currentUrl = driver.getCurrentUrl();
Assert.assertTrue(currentUrl.contains("simple-form-demo"),
"URL does not contain 'simple-form-demo'");
// 4. Create a variable for the string
String message = "Welcome to Lambda Test";
// 5. Enter the value in "Enter Message" text box
WebElement messageBox = driver.findElement(By.id("user-message"));
messageBox.clear();
messageBox.sendKeys(message);
// 6. Click "Get Checked Value"
driver.findElement(By.id("showInput")).click();
// 7. Validate the same text is displayed
String displayedMessage = driver.findElement(By.id("message")).getText();
Assert.assertEquals(displayedMessage, message,
"Displayed message does not match input message");
}
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
public static void main(String[] args) throws MalformedURLException, InterruptedException {
BasicAuthentication test = new BasicAuthentication();
test.setup();
test.authentication();
test.tearDown();
}
}