我想使用javascript自动化一个带有selenium web驱动程序的网站。我擅长Junit (java),但不是JavaScript方面的专家。
拜托,有人能帮我在JavaScript中重写这个小代码片段吗?
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.regex.Pattern;
public class Konga_Test1 {
private Selenium selenium;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.konga.com/nefertiti");
selenium.start();
}
@Test
public void testKonga_Test1() throws Exception {
selenium.open("http://www.konga.com/nefertiti");
selenium.click("//button[@type='button']");
selenium.waitForPageToLoad("30000");
verifyEquals("Shopping Cart", selenium.getText("css=h1"));
verifyTrue(selenium.isTextPresent("Frosted Chocotastic Pop tarts"));
verifyTrue(selenium.isTextPresent("₦2,100"));
selenium.goBack();
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("₦2,100"));
String vPrd = selenium.getText("css=#product-price-1091495 > span.price");
System.out.println("Value is " + vPrd);
}
private void verifyTrue(boolean textPresent) {
// TODO Auto-generated method stub
}
private void verifyEquals(String string, String text) {
// TODO Auto-generated method stub
}
@After
public void tearDown() throws Exception {
// selenium.stop();
}
}发布于 2014-09-01 21:06:15
在elcharrua编写时,您可以在JavaScript测试中使用JavascriptExecutor。这将在Selenium浏览器会话中执行JavaScript,如果它是来自网站本身的JavaScript的话。
但是,如果您想用JavaScript编写整个测试,请查看WebDriver.JS
简单例子
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();有关更复杂的示例,请参见WebDriver.Js用户指南。
还可以看看webdriver.io,它将WebDriver.JS和Node.JS与看起来更短、更清晰的语法结合起来。
您还可以通过关于JavaScript的免费codecedemy.com课程了解JavaScript。
发布于 2014-09-01 18:08:10
是的,您需要使用JavascriptExecutor类。下面是一个滚动的例子。
public void scrollToElement(final WebElement element) {
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView(true);", element);
}https://sqa.stackexchange.com/questions/9580
复制相似问题