//我使用的包文件
import org.testng.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//这是我希望通过用户输入在chrome中执行URL的java代码。在输出时我输入选项2.在Chrome中运行URL,但它显示FAILED打开浏览器
@Test (priority = 1)
public void openBrowser() {
System.out.println("-----Select Module-----");
System.out.println("1. Firefox");
System.out.println("2. Chrome");
System.out.println("3. Internet Explorer ");
int role;
try {
role = Integer.parseInt(reader.readLine());
switch (role) {
case 1:
driver = new FirefoxDriver();
break;
case 2:
driver = new ChromeDriver();
break;
case 3:
driver = new InternetExplorerDriver();
break;
default:
//System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
driver = new FirefoxDriver();
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test (priority = 2,groups = {"TC01"},description = "User would be able to Login Successfully")
@Parameters ({ "UserName","Password"})
public void SuccessfulLogin(String userName, String passWord) {
try {
driver.get("http://180.211.114.147:97/Account/Login");
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.findElement(By.id("UserName")).sendKeys(userName);
driver.findElement(By.id("Password")).sendKeys(passWord);
driver.findElement(By.id("btnLogin")).submit();
String tmp = driver.getCurrentUrl();
if (tmp.equals("http://180.211.114.147:97/#/app/dashboard"))
{
System.out.println("Login success!!");
}
else{
System.out.println("Fail to login..");}
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
/*String Actualtext = driver.findElement(By.xpath("/html/body/div/div[2]/div[3]/div[2]")).getText();
Assert.assertEquals(Actualtext, "The username or password provided is incorrect");
*/
//driver.quit();
} catch (Exception ex) {
}
}
这是XML文件的代码
<suite name="Test Login" verbose="2">
<test name="Browser selection" >
<classes>
<class name="Login_Test">
<methods>
<include name="openBrowser"></include>
</methods>
</class>
</classes>
</test>
<test name="Login with valid data" >
<parameter name="UserName" value="harsh"></parameter>
<parameter name="Password" value="harsh123"></parameter>
<groups>
<run>
<include name="TC01"></include>
</run>
</groups>
<classes>
<class name="Login_Test">
<methods>
<exclude name="InvalidLogin"></exclude>
<exclude name="EmptyLogin"></exclude>
</methods>
</class>
</classes>
</test>
TestNG的输出
[TestNG] Running:
E:\sumit_rana\Files\Automation\Java eclipse\FAM_Test\Login.xml
-----Select Module-----
1. Firefox
2. Chrome
3. Internet Explorer
2
FAILED: openBrowser
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at Login_Test.openBrowser(Login_Test.java:66)
答案 0 :(得分:0)
是的,这是一个简单的初始化错误。您需要在Chrome和IE上执行代码的驱动程序。
您需要设置系统属性并相应地提供驱动程序的路径:
System.setProperty("webdriver.ie.driver", "Drivers//IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.setProperty("webdriver.chrome.driver", "Drivers//chromedriver.exe");
driver = new ChromeDriver();
您可以在开关外部或内部设置系统属性,您的愿望。
您可以从此处下载驱动程序:http://www.seleniumhq.org/download/
我遵循的一个好习惯是创建一个名为&#39; Drivers&#39;在我的项目文件中并复制这些文件。这使您的项目更具可移植性。
答案 1 :(得分:0)
您需要下载chrome和IE的二进制文件,然后还需要在代码中传递二进制文件的路径。
根据您的配置,从以下链接获取chrome下载二进制文件: -
http://chromedriver.storage.googleapis.com/index.html?path=2.21/
从下面的链接下载Internet Explorer: -
http://www.seleniumhq.org/download/#mainContent(Internet Explorer驱动程序服务器)
现在使用setProperty设置二进制文件的路径,然后将其传递给chromedriver对象,如下所示: -
(a,b) -> {
if (a>b) return 1; // <-- you return a positive value always, so stream.max() thinks 5>8 :)
if (a<b) return -1;
if (a==b) return 0;
}
希望它会对你有所帮助:)。