How difficult is it to start writing test-scripts for someone who has been coding in Java? I've been hearing a lot about test-automation, selenium, test-script and so on but I cant get my hands on code-examples or something that can give me an understanding of the difficulty level?

My java skills are intermediate. I have a strong foundation and know the basic language very well.

  • if someone is willing, I would gladly appreciate real code-example of test-script in Java.

Like many aspects of programming, it's not that hard to get started, and even to write something that looks like a test. The real difficulty lies in test engineering.

If you know Java, particularly if you've used JUnit before, you can get started with Selenium in about ten minutes. If you know Maven, you can simply add the dependencies to your pom.xml as with any library, browse some of the basic documentation, and be well on your way to automating your tests. Copy and paste ad nauseum and you will rapidly have a suite of tests... that are fairly fragile, difficult to maintain, and likely to be abandoned before too long.

Engineering automated tests is a whole topic in itself, but you'll want to educate yourself on common pitfalls and the DRY principle if you want your tests to be maintainable. The Webdriver/Selenium community has rallied around the idea of PageObjects, which is a major concept that helps simplify test maintenance by, in essence, creating a layer of objects whose responsibility is knowing how to interact with your application, leaving the tests the responsibility of defining correct behavior. This means that when the application changes, you only have to adjust a few lines of code, not dozens or hundreds of test cases. If you do nothing else, read up on PageObjects.

My test scripts can be as simple as:

 * Runs before every test to ensure we're on the right page.
public void SetUp() {       
    //Navigate to the Location page
    locationPage = (LocationPage) PageFactory.initElements(driver, LocationPage.class).navigateTo();

 * Test: Verify that when given a valid zip code, the location page accepts it as valid.
 * Given I am on the location page
 * When I enter a valid zip code
 * Then the page should recognize that the zip code is valid
 * @throws Exception if the location page does not respond to the zip code entered
public void testValidZipcodeIsValid() throws Exception {

(and yes, I just noticed I forgot to type the Exception, oopsie)

Or a more complex example:

 * Test:
 * Given that I am on the product page
 * When I click the “A-Z” button
 * Then the tires should be sorted alphabetically in ascending order
 * When I click the “Z-A” button
 * Then the tires should be sorted alphabetically in descending order
public void sortAlpha() {
    Map<Criteria, Boolean> criteriaList = new HashMap<Criteria, Boolean>();
    criteriaList.put(Criteria.MULTISIZE, false);

    Vehicle vehicle = Vehicle.getRandomVehicleByCriteria(criteriaList);
    logger.info("Using vehicle " + vehicle);

    verifyPage = (VerifyPage) vehiclePage.clickNext();
    tireCoachPage = (TireCoachPage) verifyPage.clickNext();

    productPage = (ProductPage) tireCoachPage.clickNext();



| improve this answer | |
  • Can you explain your complex code in more detail. It is very confusing. Looks like a lot of overengineering in my eyes. Verifying sort order is a lot simpler than what you have done. – Kharbora Jul 27 '15 at 18:13
  • @Kharbora To me those examples are very clear, thanks to using PageObject pattern and DSL, with technical details hidden behgin. Excellent! If you believe you can do sorting tests in a simpler way, please demonstrate. – dzieciou Mar 5 '16 at 13:36

If you know java, writing selenium script won't be difficult for you. Automation script are similar o what when we write code for some functionality.

To start with selenium setup the environment for selenium and start with any simple program like hello world or prime number etc., then gradually start writing scripts for some functionality in any of your projects

sample code :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Webdriver_class

 public static void main(String[] args)
  WebDriver driver = new FirefoxDriver();

| improve this answer | |

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