1

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();
}
}
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Sep 1, 2014 at 17:29
2

2 Answers 2

3

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.

answered Sep 1, 2014 at 21:06
2

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);
}
answered Sep 1, 2014 at 18:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.