FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
selenium-proxy-integration-java/src/main/java/ProxyHelper.java at main · oxylabs/selenium-proxy-integration-java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
oxylabs
/
selenium-proxy-integration-java
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
6
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
selenium-proxy-integration-java
/
src
/
main
/
java
/
ProxyHelper.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (44 loc) · 2.23 KB
Breadcrumbs
selenium-proxy-integration-java
/
src
/
main
/
java
/
ProxyHelper.java
Copy path
File metadata and controls
54 lines (44 loc) · 2.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
net
.
lightbody
.
bmp
.
BrowserMobProxyServer
;
import
net
.
lightbody
.
bmp
.
client
.
ClientUtil
;
import
net
.
lightbody
.
bmp
.
proxy
.
auth
.
AuthType
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeOptions
;
import
java
.
net
.
InetSocketAddress
;
public
class
ProxyHelper
{
static
WebDriver
getDriver
(
BrowserMobProxyServer
proxy
,
boolean
headless
) {
ChromeOptions
options
=
new
ChromeOptions
();
// Required to work with https URLS
options
.
addArguments
(
"--ignore-certificate-errors"
);
// Visibility of the browser is controlled by the parameter headless
options
.
setHeadless
(
headless
);
// Enable proxy for Selenium
options
.
setProxy
(
ClientUtil
.
createSeleniumProxy
(
proxy
));
WebDriver
driver
=
new
ChromeDriver
(
options
);
return
driver
;
}
static
BrowserMobProxyServer
getProxy
(
String
endpoint
,
String
username
,
String
password
,
String
countryCode
) {
// The endpoint contains host and port. Split it for further use.
String
[]
endpointParts
=
endpoint
.
split
(
":"
);
if
(
endpointParts
.
length
!=
2
)
throw
new
IllegalArgumentException
(
"Endpoint should include host and port. For example, pr.oxylabs.io:7777"
);
String
updatedUser
=
updateUserForCountry
(
username
,
countryCode
);
// BrowserMobProxyServer makes it easy to communicate with Chrome.
BrowserMobProxyServer
proxy
=
new
BrowserMobProxyServer
();
// Handling http and https URLs
proxy
.
setTrustAllServers
(
true
);
// Oxylabs proxy as added to the chain of locally running proxy server
proxy
.
setChainedProxy
(
new
InetSocketAddress
(
endpointParts
[
0
],
Integer
.
parseInt
(
endpointParts
[
1
])));
proxy
.
chainedProxyAuthorization
(
updatedUser
,
password
,
AuthType
.
BASIC
);
// This is local proxy in JVM. Port is assinged automatically.
// It must be stopped using stop() method before exiting.
proxy
.
start
(
0
);
return
proxy
;
}
private
static
String
updateUserForCountry
(
String
userName
,
String
countryCode
) {
if
(
countryCode
==
null
) {
return
userName
;
}
return
String
.
format
(
"customer-%s-cc-%s"
,
userName
,
countryCode
);
}
}
Back
|
FazBrowse Home
|
New Git URL