Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Monday, May 25, 2015

Debugging Maven Project (IntelliJ IDEA)

How to debug your Maven test project?

Well, the command can be found here...
http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html

But the next question is always... "How can I apply it in my IDE?" It's easy to apply it in command line, but of course, you want to be able to debug your project in an IDE. :)

So we'll use this command and add a configuration in IntelliJ
mvn -DforkCount=0 test

In your IntelliJ go to Run > Edit Configurations
Then add a new maven configuration, by clicking the plus sign in the upper left corner.

There are 4 tabs available: Parameters, General, Runner, and Logs

In Parameters tab:
  • Working Directory: <Is your project directory>
  • Command Line: test
  • Profiles: <blank, unless you have a profile>
Go to Runner tab
  • VM Options: -DforkCount=0
  • JRE: Use the jdk version of your project
Click Apply, then Ok.

After this, you now can debug your Maven test project.

So for other configurations, if you want to add more parameters, you know now where to go and add those parameters. All command of maven should be on Command Line, while parameters goes in parameters tab. ;)
I hope this helps!

Wednesday, May 20, 2015

Maven Project in Mac OSX & Importing in IntelliJ

For the sake of not repeating the tutorials that are already available online, and they are good tutorials if you will take time and read it. I'm going to list down the helpful links first before writing down the things I've done to create my Maven project in Mac and converting my java project to a "mavenized" one.

  • http://www.mkyong.com/tutorials/maven-tutorials/
  • http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
  • Combine the resources and tutorial you'll find in these 2 links and you should be able to understand what Maven is and why create a project in Maven.
Pre-requisite: Installed Maven in your machine, how to know if you have maven? mvn -version
If it didn't show any maven version, then you have no maven installed.

I'm using IntelliJ Community Edition 14. I'm using this instead of eclipse because when my friend was having a problem with pom.xml, eclipse didn't return any error but just compiled the project without returning any error. On the other hand, IntelliJ returned the error about the pom.xml and my friend was able to proceed with the project. Since then, we've been using IntelliJ in creating our Selenium Webdriver scripts.

You can actually create a maven project straight from IntelliJ, but I won't go through that since that's pretty much documented in IntelliJ's website. So I'll be documenting the steps I did to create a maven project, import it in IntelliJ and then transfer all my java classes from Java project to a Maven project.

  1. Run your terminal / console
  2. Go to the directory where you want your maven project created
  3. Generate maven project: Type this to your terminal without quotes "mvn archetype:generate"
When you press enter, this command you type is not complete to create a maven project, so maven will have to ask you some things first before it can proceed.

It'll ask you a number or filter you want to apply, something like this...
$ Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 604: 

If you know the number you want for the project, then type away. But if you don't know, you can simply copy the number it's showing. So in this case, the number 604. Based on my understanding, and what happened after I've created the project, the number that was shown was the archetypeID for a quickstart project in maven.

Then you'll be asked for the following details too...

Group ID: package where you want your class to run (example: org.company.bank)
Artifact ID: this is your project name (example: TestBank)
Snapshot 1.0: version of your maven, you can press enter and leave it blank or put the desired number
Package: you can leave it blank if you want your group id as your package already

Then to finish, enter "Y" to proceed, or "N" if you want to update/change something.

And all these steps can all be summarized to this one liner code...
$ mvn archetype:generate -DgroupId=org.company -DartifactId=TestBank -Dversion=1.0-SNAPSHOT  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  • Run your IntelliJ and click on File > Import Project
  • Choose or look for your maven project and click OK.
  • Choose Maven and click next.
  • If you have any specific project settings you need to adjust, you can do it now, or do it later. Click next.
  • Then select your maven project and click next.
  • Choose the SDK version the project will use
  • Finally, the project name and click finish.
Now when you open your project, you might need to double check if your Maven in IntelliJ is pointing to the right location of maven. You can choose to override it and point it to your Maven's home. You can find where your maven is located when you do the mvn -version in ternminal. You can check with IntelliJ how to look for maven properties.

Now, next major thing you have to sort or fix is the pom.xml, why? Because maven needs the settings declared in pom.xml to build / run the whole project. Without proper pom.xml, your maven project won't run.

What's the quickest way to understand the pom.xml? Think of all your libraries or modules settings in your java project that you have to setup before compiling your project, those libraries have to be in your maven project too. But wait! You don't do it like in java project, because remember it's Maven project. So you have to use the pom and declare dependency to each libraries you have placed or used in your java project.

Quick example in my case...
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.modirum.generic.webdriver</groupId>
  <artifactId>Selenium-Webdriver-3ds</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Selenium-Webdriver-3ds</name>
  <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testing.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
As you can see here, you can get all these dependencies in http://mvnrepository.com/
You search for your library or jar, you click the project, click the latest version and then copy paste the dependency to your pom.xml

You'll see testng 6.9.4, log4j 1.2.17, and selenium-java 2.45.0
Then I have a surefire plugin to execute my testing.xml suite file. The plugins are not necessary if you don't need it yet, but like in my case, I've been using testing.xml in my java/testng project, so I need to add a plugin that will allow me to call testing.xml.

And every time you delete or add dependency, you'll notice that maven downloads the library immediately and stores it in your local maven repository.

You'll see your IDE loading whenever you add or remove a dependency. Initially the version of the dependency will be marked as red, that only means it's not yet available in your local repository, but once it's downloaded the red color of the font will become black, signifying that the download was successful.

And to test or run your maven project, you have to go to Run > Edit Configurations
and configure a maven command. For example, I want to run "mvn test" I'll have to create this setup...
  • Type a run config name: maven test
  • Working Directory: <my maven project>
  • Command line: test
  • Profiles: <blank>
  • Click apply, then run.
Then one last step is importing your java tests files to maven.
In maven you have to follow certain file structures or directory layout, you should have seen it, because it's one of the link I mentioned above.

The src/main -> is basically where all your application and files related to application should go
while src/test -> is where our test web driver scripts should go

Why is this structured this way? As much as possible, maven wants to standardized the structure of the project. The archetype that was mentioned earlier are like templates for the kind of project you want your maven structure to pattern with. So, just setup the package you need under src/test and then copy and paste the java files to the right directory / package.

With IntelliJ, you don't have to one by one open your files and change the package name from your old java project to the new one, because it can do that for you. So all you have to do is copy, paste, wait for IDE to finish, and viola! You now have a "mavenized" project!

Typically when you do your first mvn test, you get some errors, but that is if you've missed some things like path to your csv or xls file. Or you have missing dependency in pom.xml and so on... but if all is good, it should run. So learn to read the error it's throwing, so you know what's the problem. And most likely the error you'll encounter has been encountered by other people so you'll find the answer when you google it. ;)

I hope this helps, and have fun with your project! :D

Tuesday, October 28, 2014

Maven~izing your WebDriver/TestNG Project

Downloaded Maven by following it's instructions http://maven.apache.org/download.cgi
Installed it in Windows7 and installed it also in eclipse.

Went to my selenium workspace and created a folder for my first maven project.
Copy and pasted this command

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

com.mycompany.app -> is the group id or package in your java project, you can change this
my-app -> is the project name

This command was pasted in command line in windows inside the maven project.
It will download the needed maven files if it's your first time installing it.

Source Packages in Maven Project
src/main/java - is where you put all your core classes and utilities
src/tset/java - is where you put your test classes like WebDriverTest

They key in Maven project of course is the pom.xml. Without POM you cannot build your maven project. But you have to make sure you right the pom properly. I learned pom.xml by following the tutorial of Software Forum Testin in youtube and below is the sample pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>
com.selenium.app</groupId>
  <artifactId>
myseleniumtests</artifactId>
  <packaging>
jar</packaging>
  <version>
1.0-SNAPSHOT</version>
  <name>
myseleniumtests</name>
  <url>
http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>
junit</groupId>
      <artifactId>
junit</artifactId>
      <version>
3.8.1</version>
      <scope>
test</scope>
    </dependency>
    <dependency>
    <groupId>
org.seleniumhq.selenium</groupId>
    <artifactId>
selenium-server</artifactId>
    <version>
2.43.1</version>
    </dependency>
    <dependency>
    <groupId>
org.testng</groupId>
    <artifactId>
testng</artifactId>
    <version>
6.8.8</version>
    </dependency>
  </dependencies>
  <build>
 <plugins>
  <plugin>
  <groupId>
org.apache.maven.plugins</groupId>
  <artifactId>
maven-compiler-plugin</artifactId>
  <version>
3.2</version>
  <configuration>
  <source>
1.6</source>
  <target>
1.6</target>
  </configuration>
  </plugin>
  <plugin>
  <groupId>
org.apache.maven.plugins</groupId>
  <artifactId>
maven-surefire-plugin</artifactId>
  <version>
2.4.2</version>
  <configuration>
  <suiteXmlFiles>
  <suiteXmlFile>
testing.xml</suiteXmlFile>
  </suiteXmlFiles>
  </configuration>
  </plugin>
  </plugins>
  </build>
</project>
As you can see, pom.xml is pretty straight forward. If you understand what the file is for you'll understand its content. This file is what is needed by maven to build the project and run it. Above are dependencies I want my build to run with including the plugins.

But I had problem identifying the versions of the plugins I'm using and had several errors in my build. The solution is found here: http://search.maven.org/#browse

You can look for the plugin name and see the version, so you can put it in your pom.xml.

Then if you encounter error, make sure you either run
mvn clean install
or update maven project via eclipse
above did the trick for me after I carelessly deleted my .m2 folder.

Monday, October 27, 2014

Selenium Training Session Notes

Selenium Training Session
by: Software Testing Forum

******************** Video 1 Notes ********************

To test xpath you can use firebug or chrome (press F12 then ESC key)
$x -> for xpath commands
$$ -> CSS Locator

Sample page or test page: http://www.wikipedia.org/
Experiment selection for input field of search and language selection dropdown

// -> signifies or shows DOM properties (read further)
select -> html tags
@ -> is used to call the properties inside the tag
For more xpath functions: http://www.w3schools.com/xpath/xpath_functions.asp

Below are xpath samples:

If the ID is static and you want to select the input field with its id
$x("//input[@id='searchInput']")

This example below shows you that id and name can be used to identifiy the input field
$x("//input[@id='searchInput'][@name='search']")

If you are only sure of the start and other data keeps on changing
or you want to select those that starts-with, below is an example
$x("//input[starts-with(@id, 'search')]")

Here's how to use a substring if you want to base your selection on the portion of the
field's name or id or other tags
$x("//input[substring(@id, 2)='earchInput']")

Here's another example on how to use a substring with ending
$x("//input[substring(@id, 2, 5)='earch']")

If you simply want to select the field with certain pattern and can't be captured
by starts-with and substring, there is also contains function
$x("//input[contains(@id, 'npu')]")

Using following-sibling function will help you select what's beside the item you
just selected if there's no id or name you can use to select it
$x("//input[contains(@id, 'npu')]/following-sibling::select")

If you want to select the parrent it belongs to you can use /parent or /..
If you've noticed for .. it's like the command line in windows, y
$x("//input[contains(@id, 'npu')]/parent::fieldset")
$x("//input[contains(@id, 'npu')]/../..")

$x("//input[contains(@id, 'npu')]/../../../following-sibling::div")
$x("//input[contains(@id, 'npu')]/../../../following-sibling::div[3]")

$x("//select[@id='searchLanguage']/preceding-sibling::input")

Below are some samples of CSS Locator
$$ ("input[id='searchInput']")
$$ ("input[id='searchInput'][name='search']")
Not all xpath codes will work if you simply remove the \\ and @ sign
you wil still have to check the proper syntx for css selector.
Above example just quickly shows you the main difference between xpath codes.

******************** Video 2 Notes ********************

Will list all span under label (we use single / because span is direct child of label)
$x("//label[@for='langsearch-input']/span")

CSS Locator
$$("div[class='langlist langlist-large']>span")
>span  is what you add if you want to list down all direct span child of label

Will return the first span child in hierarchy
$$("div[class='langlist langlist-large']>span:nth-child(1)")

works same like nth-child but just ignores the hierarchy
$$("div[class='langlist langlist-large']>span:nth-of-type(1)")

If you want to select the ID you can also use # instead of typing ID
$$("#searchInput")

If it's not a direct childd and you want to select it
(from wikepedia selenium software search result)
$$(".mw-search-results li:nth-of-type(1) a")

Download TestNG framework : http://testng.org/doc/download.html
TestNG allows panel execution of test
TestNG allows execution of failed test which is not in JUnit

testing.xml

<suite name="Wikipedia Test" verbose="3" parallel="tests">
parallel
- methods : if you want to execute per method
- tests : if you want both executed at the same time

driver.close - will free the memory
driver.quit - will quit the browser

******************** Video 5 Notes ********************

Javascript methods in test - never use it unless it's really necessary
Document Object Model (DOM)
-You can use to navigate to each elements
-returns value with same element name in an array
--document.getElementsByName('firstName')
-this is how you get the first value of an array
--document.getElementsByName('firstName')[0]
-while this will put value to your DOM
--document.getElementsByName('firstName')[0].value='selenium'
-below casts the driver to JavascriptExecutor
--((JavascriptExecutor)driver).executeScript("document.getElementsByName('firstName')[0].value='123'");
-and to pause the execution
--Thread.sleep(5000) //5 seconds

dependsOnGroup="regTest"
-- if you want your test executed and dependent on a group of methods
-- it is also included inside @Test(..., @dependsOnGroup="regTest")
-- then the method, to be part of the group should have groups="regTest" inside @Test

if you want to know the time executed per method you can use new GregorianCalendar().getTime();
you can add this before you execute anything in your method, and add it at the end of all the lines

WebDriver and AndroidDriver

This is my notes in installing WebDriver and AndroidDriver
As of the moment these are what's available
selenium-2.43.1
adt-bundle-windows-x86_64-20140702
eclipse (I'm using the one the came with adt bundle)

Followed instructions found in https://code.google.com/p/selenium/wiki/AndroidDriver

For your SDK, you can download and install any Android version that you need. I've actually downloaded several versions like Android 5.0 (API 21) down to Android 4.0 (API 14). I just need the versions for my testing purposes.

I'll be using API 19 as my emulator in running my WebDriver.

When I reached the section Run the Tests in AndroidDriver website, eclipse can't seem to find
import org.openqa.selenium.android.AndroidDriver;


Where is this class?

So I went back to selenium website http://www.seleniumhq.org/download/
to check the Javadoc and didn't find the AndroidDriver I needed. So I went to google download list and searched for all available items to download and then sorted it by date (DESC).

https://code.google.com/p/selenium/downloads/list

I've downloaded this version, selenium-java-2.39.0, which is deprecated already. I downloaded it anyway since I just want to know if the project will build without error. So I've imported the jars and successful built it.

Selenium Change Log (2.40 and up)
If you've noticed for selenium version higher than 2.39 the AndroidDriver was removed in Selenium. If you check the log, they did that so that if you want to use the android driver you'll have to use Selendroid.