2

I have a scenario in which I need to execute the selenium scripts through servlets. I mean I will get the same input from the front end (Web UI) by passing those values to selenium classes then I will execute those classes.

I have tried this:

  1. Created a servlet

  2. Created an object for selenium class and called the main function in the servlet post method

But unfortunately, I am not getting any result or exceptions.

2

I have also added another solution which you wanted to implement ie) passing the values from the servlet post method to your selenium test

Download selenium java client driver here

Extract the downloaded jar and add the selenium-java-2.46.0.jar and all the other jars in libs folder into /WEB-INF/lib folder of the deployed web app

I will add a basic script which i tested and was working fine as an example

Servlet post method

protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
    {

        resp.setContentType("text/html");

        String url=req.getParameter("url");

       //add the parameters to be passed to the main method of the selenium test into a string array

        String[] urls=new String[1];

        urls[0]=url;

        //pass the string array as an argument to the main method

        Seleniumclass.main(urls);

    }
}

SeleniumTest

    public class Seleniumclass {

        public static void main(String[] args) {

            WebDriver driver = new FirefoxDriver();

            driver.get(args[0]);
        }

}

Note : You cannot call the testng test directly from the servlet post method like main method bcause testng is a testing framework but you can write the post values from a servlet to a file and then trigger the testng.xml file from your servlet program.In your selenium script read the values from file

Trigger testng xml file

TestListenerAdapter adapter = new TestListenerAdapter();

TestNG testng = new TestNG();

List<String> suites = Lists.newArrayList();

//path to xml

suites.add("/home/vicky/testng.xml"); 

testng.setTestSuites(suites);

testng.run();

Hope this helps you...if you still face any issues kindly get back

  • Hello Vicky, Thank you very much for your answers. Your answers are worked for me – QAMember Jun 18 '15 at 4:41
  • I want to know one more thing in your answer , If my selenium class is using testng framework then how can I call my selenium classes – QAMember Jun 18 '15 at 4:43
  • Testng is a testing framework you will not be able to call it directly from your servlet post method but you can trigger the testng.xml file from your servlet post method.I have edited my above answer kindly refer it – Vicky Jun 18 '15 at 5:39
  • after using above code testng give us test report? – raghav Apr 9 '18 at 19:07
0

The best approach is save the values from the servlet to a Database.

  1. Install continuous Integration server like Jenkins and create a Java project to check for new entries in database. Set cron job for this build. Actually Jenkins has all these features you just have to specify the time interval.

Example: If the time interval is set to 10 mins the script will run for every 10 mins . If new entries are present pass the build else fail the build.

enter image description here

  1. If the build is passed then execute the selenium script. There is an option for it in Jenkins "Build after other projects are built". In the Selenium script use the values from the database.

enter image description here

  1. In the database have a column like checkbox. Initially leave it unchecked update it after the selenium script has run.By this technique you can distinguish between new entries and old entries.

Advantages of this approach (jenkins)

  • Build history of failures and successes of the project.

  • E-mail notifications can be set up. Then test reports will be sent to the specific e-mails after execution. Any build failure can be reported as well.

  • For more advantages of Jenkins look here.

Recently I've implemented a integrating servlet and Jenkins using the above technique.

Hope this helps you... if you have any queries kindly get back.

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.