我曾使用 Selenium 和 TestNG 来自动化我的基于 Web 的应用程序,
在 TestNG 中,我们有更多的测试方法,所以我将我的 chrome Driver 和 ChromeOptions 声明为 public,如下面的代码,
ChromeOptions options = new ChromeOptions(); //declared options
// WebDriver driver =new ChromeDriver();
ChromeDriver driver = new ChromeDriver(options); //declared driver
而且我尝试使用其中一个 chrome 选项 _ incognito 通过以下行以隐身模式打开浏览器
options.arguments("--incognito");
它只在一种测试方法中工作......但我写了超过 33 个测试用例我不知道如何共同声明下面的行?工作过
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
//options.addArguments("--headless");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capabilities);
ChromeDriver driver = new ChromeDriver(options);
driver.get("http://demo.com/test/simple_context_menu.html");
driver.manage().window().maximize();
String title= driver.getTitle();
Thread.sleep(5000);
driver.quit();
当我在代码下面的 void 方法中声明时,它工作正常
package QAInstallExtentionTest;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class testingImplementAreatest{
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capabilities);
ChromeDriver driver = new ChromeDriver(options);
driver.get("http://demo.com/test/simple_context_menu.html");
driver.manage().window().maximize();
String title= driver.getTitle();
Thread.sleep(5000);
driver.quit();
System.out.println("finished automation" +title);
}
}
但是当我声明相同时(在 void 方法之外)它不起作用并显示错误
package QAInstallExtentionTest;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class testingImplementAreatest{
ChromeOptions options = new ChromeOptions(); // declare chromeOptions
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capabilities);
ChromeDriver driver = new ChromeDriver(options); // declare Chrome driver
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
driver.get("http://demo.com/test/simple_context_menu.html");
driver.manage().window().maximize();
String title= driver.getTitle();
Thread.sleep(5000);
driver.quit();
System.out.println("finished automation" +title);
}
}
请帮我解决这个问题?我在哪里犯错?
提前感谢朋友和更多细节问题图片