我正在使用PhantomJs驱动程序进行无头测试,我得到以下异常
示例代码:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestLogin {
WebDriver d;
@BeforeMethod
public void launh_Browser() {
System.setProperty("phantomjs.binary.path", "D:\Selenium\driver\phantomjs-2.1.1-windows\bin\phantomjs.exe");
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
d=new PhantomJSDriver(caps);
}
@Test
public void guru_banking_login_excel() throws Exception {
d.get("http://www.demo.guru99.com/V4/");
d.findElement(By.name("uid")).sendKeys("TestUser");
d.findElement(By.name("password")).sendKeys("testpwd");
d.findElement(By.name("btnLogin")).click();
try{
Alert alt = d.switchTo().alert();
String actualBoxMsg = alt.getText(); // get content of the Alter Message
assertEquals(actualBoxMsg,"User or Password is not valid");
alt.accept();
}
catch (NoAlertPresentException Ex){
String hometitle=d.getTitle();
assertEquals(hometitle,"Guru99 Bank Manager HomePage");
}
d.quit
}
观察到的错误:
Exception : org.openqa.selenium.UnsupportedCommandException: Invalid Command Method - {"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","
我正在尝试使用phantomjs作为驱动程序来处理弹出窗口 请帮忙
...提前致谢..!!!
你必须在代码中注意很多要点:
在使用
PhantomJS
时,当您提到System.setProperty
使用以下代码行时:File src = new File("C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe"); System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities
类型的对象必须仅引用DesiredCapabilities
类来启动。所以改为:DesiredCapabilities caps = new DesiredCapabilities();
使用assertEquals
使用Assert.assertEquals
如下:Assert.assertEquals(actualBoxMsg,"User or Password is not valid"); // Assert.assertEquals(hometitle,"Guru99 Bank Manager HomePage");
当您使用
TestNG
时,对于Assert
,请使用org.testng.Assert;
而不是static org.testng.Assert.assertEquals;
作为导入:import org.testng.Assert;
您还需要将行
d.quit()
包装在一个单独的TestNG Annotated
函数中,如下所示:@AfterMethod public void tearDown() { d.quit(); }
这是你自己的代码块,它成功执行:
package headlessBrowserTesting; import java.io.File; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class PhantomJS_webdriver_binary { WebDriver d; @BeforeMethod public void launh_Browser() { File src = new File("C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe"); System.setProperty("phantomjs.binary.path", src.getAbsolutePath()); DesiredCapabilities caps = new DesiredCapabilities(); ((DesiredCapabilities) caps).setJavascriptEnabled(true); ((DesiredCapabilities) caps).setCapability("takesScreenshot", true); d=new PhantomJSDriver(caps); } @Test public void guru_banking_login_excel() throws Exception { d.get("http://www.demo.guru99.com/V4/"); d.findElement(By.name("uid")).sendKeys("TestUser"); d.findElement(By.name("password")).sendKeys("testpwd"); d.findElement(By.name("btnLogin")).click(); try{ Alert alt = d.switchTo().alert(); String actualBoxMsg = alt.getText(); Assert.assertEquals(actualBoxMsg,"User or Password is not valid"); alt.accept(); } catch (NoAlertPresentException Ex){ String hometitle=d.getTitle(); Assert.assertEquals(hometitle,"Guru99 Bank Manager HomePage"); } } @AfterMethod public void tearDown() { d.quit(); } }