gpt4 book ai didi

java - 如何与实现 ITestListener 的 TestNG 结合使用 Firefox 驱动程序

转载 作者:行者123 更新时间:2023-11-30 07:13:56 25 4
gpt4 key购买 nike

非常感谢您在这里提供任何建议。我正在尝试使用 TestNG 并实现 ITestListerner 接口(interface)来使用 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 和覆盖 failure public void onFinish(ITestContext result) 来使用与 TestNG 相同的驱动程序,不幸的是,它不会返回 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());
}
}

您能否建议一种方法,以下方法不应启动新的浏览器 session ,但焦点应保留在第一个浏览器实例上并捕获屏幕截图

最佳答案

您需要将 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());

关于java - 如何与实现 ITestListener 的 TestNG 结合使用 Firefox 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com