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();
}
}
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.

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.