Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
hofmann
Active Contributor

Before an application can go into production, it needs to be tested. For Java there are several widely adopted test frameworks available like jUnit or TestNG. As these are used to test Java applications, what about web applications? How to test the UI that actually is constructed by using HTML, CSS, JSP and Javascript? This is not easy as you have to simulate user input and validate the generated response, including different browsers. The example in this blog is about testing a simple HTMLB checkbox.

Record the actions

A tool that can help you creating test cases for web applications is Selenium. Selenium offers a Firefox plugin called Selenium IDE that records what the user is clicking and translates these actions into HTML code. To test an application with Selenium the following steps are needed:

  1. Record the user action
  2. Select a test framework and save the recorded actions in Java code
  3. Import the generated code into your Java project

For the 1st step, Selenium offers a Firefox plugin called Selenium IDE. This means while the user actions can be replayed by several browsers (IE, Opera, FF, mobile, etc.), with the IDE only the user actions from a Firefox user can be recorded.

In the generated HTML code of Selenium, these values are shown as HTML:

<tr>
    <td>verifyText</td>
    <td>//html/body/table/tbody/tr/td/form/span[2]/label</td>
    <td>Simple checkbox checked</td>
</tr>
<tr>
    <td>verifyElementPresent</td>
    <td>//html/body/table/tbody/tr/td/form/span[2]/label/img[@class='urCImgOn']</td>
    <td></td>
</tr>

It is easy to see a possible problem here: how Selenium will find the actual HTML UI to test. In the above example the absolute path is used. When the portlet is not run as a single application but inside the portal the path will change. So make sure that the HTML code of the application is surrounded by a unique HTML that can serve as a starting point of the path.

As the HTML isn’t really useful, you’ll have to export the actions to a framework of your choice (I created my own export template called SAPHtmlbTestNG).

For those who already know Selenium: the IDE does not come with an export to TestNG and WebDriver, so you are bound to use Selenium RC. After exporting the test case you get Java code that actually can be used to run the tests:

public class test extends Tests {
    @Parameters({"seleniumServer", "seleniumPort", "seleniumBrowser", "testBaseUrl"})
    public test(String seleniumServer, int seleniumPort, String seleniumBrowser, String testBaseUrl) {
        super (seleniumServer, seleniumPort, seleniumBrowser, testBaseUrl);
    }
    @BeforeClass
    public void setUp() throws Exception {
        selenium.start();
    }
    @Test public void testTest() throws Exception {
        selenium.open("/irj/servlet/prt/portal/prtroot/com.tobias.test.testSelenium.testCheckbox");
        […]
        verifyTrue(selenium.isElementPresent("//html/body/table/tbody/tr/td/form/span[2]/label/img[@class='urCImgOn']"));
        selenium.click("//html/body/table/tbody/tr/td/form/span[2]/label/img");
        […]
    }
    @AfterClass
    public void tearDown() throws Exception {
        selenium.stop();
    }
}

What does that actually mean? In the Selenium IDE I added a test that checks if a HTML element (e.g. a UI element like a button, checkbox or any other HTML element) is in the HTML code retrieved from the application:

verifyElementPresent

For this Selenium identified the actual location of the element in question in the HTML source code:

//html/body/table/tbody/tr/td/form/span[2]/label/img[@class='urCImgOn']

Transformed into Java code, this reads as:

verifyTrue(selenium.isElementPresent("//html/body/table/tbody/tr/td/form/span[2]/label/img[@class='urCImgOn']"));

As for Selenium and testing: the actual Java code for executing the tests is Selenium. TestNG only serves as the framework running the Selenium Java code. That’s why there are testing annotations like @BeforeClass. TestNG will control the flow of the tests (starting selenium, running the tests, stopping selenium).

Running the Selenium test with TestNG

To run the tests a few things have to be considered:

  1. A Selenium server needs to be up and running
  2. TestNG can be controlled over a XML file

Starting the Selenium server can be done via a (ant/maven) script or manually. What is more interesting is the XML file that controls TestNG. There the actual tests to be performed together with parameters are defined:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
    <parameter name="seleniumServer" value="localhost" />
    <parameter name="seleniumPort" value="4444" />
    <parameter name="seleniumBrowser" value="*chrome" />
    <parameter name="testBaseUrl" value="http://server:port/" />
    <parameter name="logonUser" value="test" />
    <parameter name="logonPassword" value="abc" />
    <listeners>
        <listener class-name="com.tobias.test.selenium.Screenshot" />
    </listeners>
      <test name="Test" preserve-order="true">
        <classes>
          <class name="com.tobias.test.selenium.checkbox.Checkbox"/>
        </classes>
      </test>
</suite>

The parameters are read by the annotation @Parameters and are used to define the selenium server. I added a listener that allows me to take a screenshot when a test fails and in the <test> section the actual tests are listened. There is only one test listened; to add more tests just add them by <class> definitions.

Run the test

After the tests are prepared and the tests and parameters are defined in the XML, the tests can run: either by invoking them directly in Eclipse or by running a script. TestNG will create HTML reports of the tests results.

Adding new test cases isn`t really hard when using the Selenium IDE. Where the IDE doesn`t help creating test cases, the developer can create them directly in Java. A benefit of Selenium is that you`ll end up having functional tests, as a real browser will make the calls to the application and validate the result.

From the 21 tests in my test suite, 2 failed. Because of my screenshot class added to my test suite, TestNG made a screenshot when the test failed.

Selenium used together with a framework like TestNG allows for functional testing of SAP web application. This is not bound to SAP Portal or WDJ apps, everything that has a HTML based UI can be tested. You have to keep in mind that even as UI testing is important, you should not focus solely on it. Unit and service tests should make the largest part in your test efforts. You won't get OOTB a lot of UI tests, that process needs some time. It would be nice to get from SAP a predefined set of tests that can be run with every patch/ EHP installation, but it is up to you to create these tests.

11 Comments
gregorw
Active Contributor
0 Kudos

Hi Tobias,

great to see others picking up Selenium. As you might be aware I've created the SeleniumABAP project on Code Exchange which allows you to run tests from within the ABAP Stack as ABAPUnit or eCATT tests. You can watch a replay of my talk at Mastering SAP Technologies.

Best regards

Gregor

Former Member
0 Kudos

Hi Tobias,

Excellent job!


In Brazil I automate SAP testing with Selenium RC.

I wrote a post about this job.

http://www.qualidadedesoftware.com.br/2013/10/testando-sap-com-selenium.html

Best regards

Eduardo Souza

former_member196942
Participant
0 Kudos

Hi Tobias/All,

                I am working on SAP Selenium webdriver. I wanted to know that after every test

                when the HTML reports are generated,how can we mail the same to the                             concerned person?

                Searched for it on the net but couldn't find.

               Any help will be greatly appreciated.

Best regards,

Priya Jha

Former Member
0 Kudos

Hi Priya ,

Its good to use Jenkins, there you can not only schedule nightly builds but aslo reports can be automatically triggered to respective person.

Thanks,

Abhishek

Former Member
0 Kudos

Hi Priya/Gregor/All,

I also started working on CRM WEB UI with selenium webdriver.

I have a few questions over here.

I want to export a value from selenium wedriver and import same value in sap tools like eCATT/CBTA and proceed the test. ( It's like integration scenario which consist of CRM UI using selenium webdriver, SAP GUI using eCATT/CBTA in a single test ).

Can you kindly help me in this.

Regards,

Siva

Former Member
0 Kudos

Hello Tobias

I got your point, thanks for the post.

I am working as a selenium tester in a small size firm. I was always wondering how to test SAP product using selenium automation testing tool here.

Former Member
0 Kudos

Hi Eduardo,

I am not able to access you link.

Could you please provide me correct link.

In my new project, we have to automate SAP, hence i am looking for option if we can do that using selenium + java.

Your help is much appreciated.

Thanks,

-Mangla

Former Member
0 Kudos

Hi Tobias,

In my new project, we have to automate SAP web application, hence i am looking for option if we can do that using selenium + java.

Your help is much appreciated.

Thanks,

-Mangla

Former Member
0 Kudos
hi Mazhar

Did you use Selenium IDE , how did you get firefox to recognize the SAP object

is the format SAPHtmlbTestNG required

 
Former Member
0 Kudos
Is it for SAP UI5?
Loraine
Explorer
0 Kudos
Tobias,

Sabe me informar se a ferramenta Sellenium, server para tb utilizarmos no SAP com o conceito de esteira (DevOps)?

Obrigada,

Loraine Sant´Ana
Labels in this area