-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampleBM.java
More file actions
143 lines (97 loc) · 4.28 KB
/
SampleBM.java
File metadata and controls
143 lines (97 loc) · 4.28 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
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.lambdatest.tunnel.Tunnel;
public class SampleBM {
public RemoteWebDriver driver;
public BrowserMobProxy proxy;
Tunnel t;
@org.testng.annotations.Parameters(value = {"browser", "version", "platform"})
@BeforeTest
public void setup(String browser, String version, String platform) throws Exception {
proxy = new BrowserMobProxyServer();
System.out.println("Browser Mob proxy--------"+proxy);
proxy.setTrustAllServers(true);
proxy.start();
// start the proxy
String portn = String.valueOf(proxy.getPort());
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
System.out.println("Selenium Proxy-------"+seleniumProxy);
String hostIp = Inet4Address.getLocalHost().getHostAddress();
seleniumProxy.setHttpProxy(hostIp + ":" + proxy.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + proxy.getPort());
System.out.println(portn + " <-- BrowserMob Proxy port passed in tunnel");
//initiating tunnel instance
t = new Tunnel();
HashMap<String, String> options = new HashMap<String, String>();
options.put("user", "your_LT_username");
options.put("key", "your_LT_accesskey");
options.put("proxyHost",hostIp);
options.put("proxyPort", portn);
options.put("ingress-only", "--ingress-only"); //mandatory while using BM proxy
options.put("tunnelName",portn);
//start tunnel
t.start(options);
ChromeOptions option = new ChromeOptions();
option.addArguments("--ignore-certificate-errors");
option.setProxy(seleniumProxy);
option.setAcceptInsecureCerts(true);
option.addArguments("--disable-backgrounding-occluded-windows");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, option);
capabilities.setCapability("browserName", browser);
capabilities.setCapability("version", version);
capabilities.setCapability("platform", platform);
capabilities.setCapability("tunnel",true);
capabilities.setCapability("tunnelName",portn);
driver = new RemoteWebDriver(new URL("https://username:accesskey@hub.lambdatest.com/wd/hub"),capabilities);
}
@Test
public void test() throws Exception {
driver.manage().window().maximize();
proxy.newHar("HomePage");
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("LambdaTest");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
driver.get("https://lambdatest.com");
// get the HAR data
Thread.sleep(5000);
Har har = proxy.getHar();
//System.out.println("------------------------------------>"+har.getLog());
try {
har.writeTo(new File("C://Users//deepanshu//Downloads//Java-TestNG-BrowserMobProxy-master 2//Java-TestNG-BrowserMobProxy-master"+proxy.getPort()+"homepage.har"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
@AfterMethod
public void tearDown() throws Exception {
if (driver != null) {
try {
driver.quit(); //quitting the driver instance
proxy.stop(); //stop proxy
t.stop(); //stop tunnel
}catch(Exception e){
System.out.println(e);
}
}
}
}