英文:
Selenium Test from Unix to Windows
问题
在我的Spring Boot应用程序中,我有一个控制器(Controller),其中有一个名为startTest的端点,该端点调用了作为JAR包的Selenium测试包中的Test.jar。
Test.jar --> 类TestSuite --> 方法:LG0000001 使用 WebDriver 类来模拟浏览器(Chrome)上的自动化操作。
这两者都被打包为JAR文件,并部署在类似Test.jar和BackEnd.jar的Unix服务器上。
我从Windows上的Chrome浏览器中调用startTest服务,如下所示:
10.233.233.235:8080/startTest/TestOther.jar/TestSuite/LG0000001
这是我的后端类:
@RestController
public class Controller {
@RequestMapping(value = {"/startTest/Test.jar/TestSuite/LG0000001"}, method = RequestMethod.GET)
public ResponseEntity<Esito> runTest(@PathVariable String jar, @PathVariable String class_name, @PathVariable Optional<String> test_name) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, MalformedURLException, InstantiationException, IllegalArgumentException, InvocationTargetException {
ReportInnerTests listener = new ReportInnerTests(); // 这是一个监听器的重写
this.runOne(jar, class_name, test_name, listener); // 用于运行Test.jar中的测试的方法
Map<String, String> summary = listener.getSummary();
HttpHeaders responseHeaders = new HttpHeaders();
Esito esito = new Esito();
esito.setSummary(summary);
return new ResponseEntity<Esito>(esito, responseHeaders, HttpStatus.OK);
}
public void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener) throws ClassNotFoundException, NoSuchMethodException, MalformedURLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Launcher launcher = LauncherFactory.create();
ClassLoader loader = URLClassLoader.newInstance(
new URL[]{new File(pathJars + "/" + jar).toURI().toURL()},
getClass().getClassLoader()
);
loader.getClass();
Class cls = Class.forName(class_name, true, loader);
Constructor constructor = cls.getConstructor();
constructor.newInstance();
LauncherDiscoveryRequest request;
if (test_name.isPresent()) {
Method m = cls.getMethod(test_name.get());
request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectMethod(cls, m))
.build();
} else {
request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(cls))
.build();
}
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request); // 在这里运行测试,对于不使用Selenium的其他测试有效
}
}
Selenium的测试类在我的Windows上运行正常:
@ExtendWith(TestReporter.class)
public class TestSuite {
WebDriver driver;
Actions action;
JavascriptExecutor jse;
public TestSuite() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/test_jar/chromedriver");
driver = new ChromeDriver();
action = new Actions(driver);
jse = (JavascriptExecutor) driver;
}
@Test
public void LG0000001() {
driver.manage().window().maximize();
LoginTest.login(driver, "http://xxxxxx.xxx.yyy.pages/dashboard", "axfas", "str");
}
}
但是当我运行TestSelenium.jar时,出现以下错误:
org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
我的问题是我是否可以从Windows上的RestPost调用中在Unix上运行TestSelenium,并在调用它的Windows上的Chrome浏览器上查看所有浏览器上的自动化操作。
是否可能,还是一切都是无用的?
谢谢
问候
英文:
in my springboot application i have a Controller with endpoint startTest that call test Selenium package as jar.
Test.jar-->Class TestSuite --> method: LG0000001 use WebDriver class for simulate automation operation on browser (Chrome).
Both are packaged as JAR and deployed on Unix Server like Test.jar and BackEnd.jar.
I call service startTest from chrome on windows like:
> 10.233.233.235:8080/startTest/TestOther.jar/TestSuite/LG0000001
This is my Backend Class:
@RestController
public class Controller {
@RequestMapping(value = {"/startTest/Test.jar/TestSuite/LG0000001"},method = RequestMethod.GET) //this is for example i run only one method inside class
public ResponseEntity<Esito> runTest(@PathVariable String jar, @PathVariable String class_name, @PathVariable Optional<String> test_name ) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, MalformedURLException, InstantiationException, IllegalArgumentException, InvocationTargetException {
ReportInnerTests listener = new ReportInnerTests(); // is a overwrite for listener
this.runOne(jar,class_name,test_name,listener); //method for run test on Test.jar
Map<String, String> summary= listener.getSummary();
HttpHeaders responseHeaders = new HttpHeaders();
Esito esito=new Esito();
esito.setSummary(summary);
return new ResponseEntity<Esito>(esito, responseHeaders, HttpStatus.OK);
}
public void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener) throws ClassNotFoundException, NoSuchMethodException, MalformedURLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Launcher launcher = LauncherFactory.create();
ClassLoader loader = URLClassLoader.newInstance(
new URL[] { new File(pathJars+"/"+jar).toURI().toURL() },
getClass().getClassLoader()
);
loader.getClass();
Class cls=Class.forName(class_name,true,loader);
Constructor constructor = cls.getConstructor();
constructor.newInstance();
LauncherDiscoveryRequest request;
if (test_name.isPresent()) {
Method m = cls.getMethod(test_name.get());
request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectMethod(cls,m))
.build();
}
else{
request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(cls))
.build();
}
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request); //here I run Test and work well with other Test that don't use Selenium
}
classTest for selenium is this that work if i run backend on my pc with windows:
@ExtendWith(TestReporter.class)
public class TestSuite {
WebDriver driver;
Actions action;
JavascriptExecutor jse;
public TestSuite() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/test_jar/chromedriver"); //here i load driver and work on Unix
driver = new ChromeDriver();
action = new Actions(driver);
jse = (JavascriptExecutor) driver;
}
@Test
public void LG0000001(){
driver.manage().window().maximize();
LoginTest.login(driver, "http://xxxxxx.xxx.yyy.pages/dashboard",
"axfas","str"); // this a test that use another my class that use "WebDriver"
};
If i run other Test with Junit for example it work well and i view results. But when i run TestSelenium.jar i have this error:
2020-04-09 16:10:40.548 ERROR 90384 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.reflect.InvocationTargetException] with root cause
org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
My question is if i can run TestSelenium on Unix called it by a RestPost call from windows:
10.233.233.235:8080/startTest/TestOther.jar/TestSuite/LG0000001
but i want view all automation operation on Browser on Chrome on windows where i call it.
Is possible or is all useless??
Thanks
Regards
专注分享java语言的经验与见解,让所有开发者获益!
评论