- 这是一个简单的java项目。 - 我用过Testng。 - 当我通过Testng运行我的.xml文件时,我可以看到我的脚本已在远程计算机上运行。 - 但是当我创建.bat文件时,那还没有执行。 - 我正在使用Jenlen的selenium网格。所以请分享一下代码。 在我的cmd窗口上显示错误消息,如下所示: -
set classpath=D:\selenium\selenium_grid\bin;D:\selenium\selenium_grid\lib\*
new chrome options<> is preferred to DesiredCapabilities capabilities=DesiredCapabilities.chrome();
代码:
package grid_hub_node;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Demo {
WebDriver driver;
@Parameters({ "system" })
@Test
public void testdemo(String system) throws InterruptedException
{
if (system.equalsIgnoreCase("pc1")) {
try {
String nodeurl;
nodeurl="http://192.168.1.115:5090/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("https://www.ebay.in/");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}
else if (system.equalsIgnoreCase("pc2")) {
try {
String nodeurl;
nodeurl="http://192.168.1.112:6760/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("http://sdrc.co.in/");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}
else if (system.equalsIgnoreCase("pc3")) {
try {
String nodeurl;
nodeurl="http://192.168.1.102:4051/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("http://www.seleniumhq.org/");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}
else if (system.equalsIgnoreCase("pc4")) {
try {
String nodeurl;
nodeurl="http://192.168.1.121:7611/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("https://www.microsoft.com/net/download/windows");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}
else if (system.equalsIgnoreCase("pc5")) {
try {
String nodeurl;
nodeurl="http://192.168.1.177:6474/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("https://cricket.yahoo.com/");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}
else if (system.equalsIgnoreCase("pc6")) {
try {
String nodeurl;
nodeurl="http://192.168.1.141:5871/wd/hub";
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
driver=new RemoteWebDriver(new URL(nodeurl),capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.get("http://www.fifa.com/");
String title_name=driver.getTitle();
System.out.println(title_name);
Thread.sleep(7000l);
driver.quit();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}}}}
TestNG.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="10" parallel="tests">
<test name="Test1">
<parameter name="system" value="pc1"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
<test name="Test2">
<parameter name="system" value="pc2"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
<test name="Test3">
<parameter name="system" value="pc3"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
<test name="Test4">
<parameter name="system" value="pc4"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
<test name="Test5">
<parameter name="system" value="pc5"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
<test name="Test6">
<parameter name="system" value="pc6"/>
<classes>
<class name="grid_hub_node.Grid_Demo_parallel"/>
</classes>
</test>
</suite> <!-- Suite -->
bat文件:
set projectLocation=D:\selenium\selenium_grid
cd %projectLocation%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %projectLocation%\testng.xml
pause
答案 0 :(得分:0)
该错误为我们提供了以下问题的一些提示:
set classpath=D:\selenium\selenium_grid\bin;D:\selenium\selenium_grid\lib\*
new chrome options<> is preferred to DesiredCapabilities capabilities=DesiredCapabilities.chrome();
主要问题似乎是设置classpath
按照以下步骤操作:
从Project Directory,通过CLIP提供以下类路径:
C:\Users\<user_name>\<ProjectWorkspace>\<ProjectName>>set classpath=C:\Users\<user_name>\<ProjectWorkspace>\<ProjectName>\bin;C:\<user_name>\<ProjectWorkspace>\<ProjectName>\lib\*;
从CLI执行testng.xml,如下所示:
C:\<user_name>\<ProjectWorkspace>\<ProjectName>>java org.testng.TestNG testng.xml
观察Testcase是否已执行。
在创建新文本文档并添加以下代码:
java -cp bin;lib/* org.testng.TestNG testng.xml
另存为&#34; run.bat&#34;
执行脚本&amp;验证测试用例是否已执行。