I want to automate a website with selenium web driver using javascript. I am good at Junit
(java) but not an expert in JavaScript.
Please, can anyone help me rewrite this little code snippet in JavaScript to make some comparison:
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();
}
}
-
Why do you want to convert your code? That might help get you more focused answers.Yamikuronue– Yamikuronue2014年09月25日 18:10:30 +00:00Commented Sep 25, 2014 at 18:10
-
If you already know Java, stick to Java - that's webdriver's natural environment. As they say on the front page WebDriver is the name of the key interface against which tests should be written in Java.Vince Bowdren– Vince Bowdren2014年09月26日 07:33:14 +00:00Commented Sep 26, 2014 at 7:33
2 Answers 2
As elcharrua writes you can use JavaScript in Java tests with the JavascriptExecutor. This will execute the JavaScript in the Selenium browser session if it was JavaScript from the website itself.
But if you want to write the whole test in JavaScript look at WebDriver.JS
Simple example
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();
For more complex examples see the WebDriver.Js user guide.
Also look into webdriver.io which combines WebDriver.JS and Node.JS with what looks like a shorter and clearer syntax.
You can also follow the free JavaScript course on codecedemy.com for an introduction to JavaScript.
Yes, you need to use JavascriptExecutor Class. Below is an example to scroll.
public void scrollToElement(final WebElement element) {
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView(true);", element);
}
Explore related questions
See similar questions with these tags.