我很感激并感谢这里的任何建议。我试图通过使用TestNG和实现ITestListerner接口从POM框架中使用驱动程序对象。
这是一个实现ItestListener的TestNG类
public class TestNGListener implements ITestListener {
@Override
public void onFinish(ITestContext result) {
WebDriver driver = BrowserFactory.LanuchBrowser("firefox", "http://10.207.182.108:81/opencart/");
Util.takescreenshot(driver, result.getName());
}
此类用于在从switch case启动其中一个浏览器后返回WebDriver对象。我通过实现Itestlistener& amp;而使用相同的驱动程序作为TestNG的一部分。在上面的类&中覆盖失败public void onFinish(ITestContext result)不幸的是,它没有返回webdriver对象&拍摄屏幕截图,但改为启动新的浏览器。
public class BrowserFactory {
static WebDriver driver;
public static WebDriver LanuchBrowser(String Brwsr, String URL){
System.out.println(Brwsr.toLowerCase());
switch (Brwsr.toLowerCase()){
case "firefox":
driver=new FirefoxDriver();
break;
case "chrome":
driver= new ChromeDriver();
System.setProperty("WebDriver.chrome.driver", "chromedriver.exe");
break;
case "internet explorer":
System.out.println("IE");
driver= new InternetExplorerDriver();
System.setProperty("WebDriver.IE.driver","IEDriverServer.exe");
break;
default:
System.out.println("Please select one of the Browsers listed : Chrome,Firefox or InternetExplorer");
break;
}
driver.manage().window().maximize();
driver.get(URL);
return driver;
}
}
这是我捕获屏幕截图的方法
public class Util {
final static Logger logger = Logger.getLogger(Util.class);
public static void validatePgeNavgtn(WebDriver driver, String PgeTitle){
PropertyConfigurator.configure("log4j.properties");
String pgtitle=driver.getTitle();
if (pgtitle.equalsIgnoreCase(PgeTitle)){
logger.info("title matched");
}
}
public static void takescreenshot(WebDriver driver, String screen){
TakesScreenshot ts = (TakesScreenshot)driver;
File src= ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(src, new File("./Screenshot/"+screen+".png"));
} catch (IOException e) {
logger.warn(e.getMessage());
}
}
请您建议一种方法,即以下方法不应启动新的浏览器会话,但重点应保留在第一个浏览器实例&还捕获截图
答案 0 :(得分:1)
您需要创建BrowserFactory
类作为单例,它将创建始终提供BrowserFactory
的单个实例,如下所示: -
public class BrowserFactory {
private WebDriver driver;
private static BrowserFactory browserFactoryInstance = null;
private BrowserFactory(String Brwsr, String URL) {
System.out.println(Brwsr.toLowerCase());
switch (Brwsr.toLowerCase()) {
case "firefox":
this.driver = new FirefoxDriver();
break;
case "chrome":
System.setProperty("WebDriver.chrome.driver", "chromedriver.exe");
this.driver = new ChromeDriver();
break;
case "internet explorer":
System.out.println("IE");
System.setProperty("WebDriver.IE.driver", "IEDriverServer.exe");
this.driver = new InternetExplorerDriver();
break;
default:
System.out
.println("Please select one of the Browsers listed : Chrome,Firefox or InternetExplorer");
break;
}
this.driver.manage().window().maximize();
this.driver.get(URL);
}
public static BrowserFactory getInstance(String Brwsr, String URL) {
if(browserFactoryInstance == null) {
browserFactoryInstance = new BrowserFactory(Brwsr, URL);
}
return browserFactoryInstance;
}
public WebDriver getDriver() {
return this.driver;
}
}
现在您可以获得WebDriver
实例,如下所示: -
//It will return always single instance per test run
WebDriver driver = BrowserFactory.getInstance("firefox", "http://10.207.182.108:81/opencart/").getDriver();
Util.takescreenshot(driver, result.getName());