如何在 QAF 中设置代理
How to setup proxy in QAF
我需要在我的 QAF 测试方法中设置代理。下面的代码在没有 QAF 的情况下工作正常。如何与 QAF 实施设置相同?
// @Test
public void sampleTest() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
@SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities); // Here opening new window and able
to hit my localhost:8080
//i need to use following QAF methods which also needs to trigger my localhost
get("http://demo.rapidtestpro.com/login.php");
sendKeys("1234567", "//*[@id='accno']");
sendKeys("password123", "//*[@id=\"pass\"]");
System.out.println("Exit in sample test");
}
我还尝试在 application.properties 文件中设置代理,如下所示
system.http.proxyHost=localhost
system.http.proxyPort=8080
也在 Testngconfig.xml 文件中试过,如下所示
<test name="java Test" enabled="true">
<parameter name="driver.name" value="chromeDriver"/>
<parameter name="system.http.proxyHost" value="localhost"/>
<parameter name="system.http.proxyPort" value="8080"/>
localhost:8080 中没有触发任何内容。
根据您的工作代码,您想为驱动程序使用功能设置代理。当您使用 qaf 时,您可以通过不同的方式提供驱动程序功能。最简单的方法是通过设置适当的 属性 和所需功能的 json 值。在您的情况下,它将如下所示:
driver.name=chromeDriver
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]},"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
如果您想为所有浏览器设置代理功能:
driver.name=<driver name>
#additional capabilities for any driver
driver.additional.capabilities={"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
#additional capabilities only for chrome
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]}}
如果您不知道 json 功能的表示形式,您可以在控制台中打印并作为参考。例如,带有打印 json 功能值的代码,您引用的问题:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println(JSONUtil.toString(capabilities.toJson()));
}
其他方法是使用 driver listener,您可以在其中附加功能 void beforeInitialize(Capabilities capabilities)
例如:
void beforeInitialize(Capabilities capabilities){
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = (DesiredCapabilities)capabilities;
capabilities.setCapability("proxy", proxy);
}
我需要在我的 QAF 测试方法中设置代理。下面的代码在没有 QAF 的情况下工作正常。如何与 QAF 实施设置相同?
// @Test
public void sampleTest() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
@SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities); // Here opening new window and able
to hit my localhost:8080
//i need to use following QAF methods which also needs to trigger my localhost
get("http://demo.rapidtestpro.com/login.php");
sendKeys("1234567", "//*[@id='accno']");
sendKeys("password123", "//*[@id=\"pass\"]");
System.out.println("Exit in sample test");
}
我还尝试在 application.properties 文件中设置代理,如下所示
system.http.proxyHost=localhost
system.http.proxyPort=8080
也在 Testngconfig.xml 文件中试过,如下所示
<test name="java Test" enabled="true">
<parameter name="driver.name" value="chromeDriver"/>
<parameter name="system.http.proxyHost" value="localhost"/>
<parameter name="system.http.proxyPort" value="8080"/>
localhost:8080 中没有触发任何内容。
根据您的工作代码,您想为驱动程序使用功能设置代理。当您使用 qaf 时,您可以通过不同的方式提供驱动程序功能。最简单的方法是通过设置适当的 属性 和所需功能的 json 值。在您的情况下,它将如下所示:
driver.name=chromeDriver
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]},"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
如果您想为所有浏览器设置代理功能:
driver.name=<driver name>
#additional capabilities for any driver
driver.additional.capabilities={"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
#additional capabilities only for chrome
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]}}
如果您不知道 json 功能的表示形式,您可以在控制台中打印并作为参考。例如,带有打印 json 功能值的代码,您引用的问题:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println(JSONUtil.toString(capabilities.toJson()));
}
其他方法是使用 driver listener,您可以在其中附加功能 void beforeInitialize(Capabilities capabilities)
例如:
void beforeInitialize(Capabilities capabilities){
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = (DesiredCapabilities)capabilities;
capabilities.setCapability("proxy", proxy);
}