Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, March 21, 2016

Fix Unsupported major.minor version

Source: stockoverflow

Sometimes when you build your project with wrong java version and run your project, you get this kind of error, below is a guide to what java version is needed when this error occurs...

The version number shown describes the version of the JRE the class file is compatible with.
The reported major numbers are:
J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

Source: stockoverflow postgres
If you encounter this error, PostgreSQL unsupported major.minor version
java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0 (unable to load class org.postgresql.Driver)

This means that the postgres driver you're using does not support the java version used to build your project.

You can check here in postgres documentation the jdbc version you need if you're running a certain java version: https://jdbc.postgresql.org/download.html

Below are the details found in this page...

If you are using the 1.6 then you should use the JDBC4 version. If you are using 1.7 then you should use the JDBC41 version. If you are using 1.8 then you should use the JDBC42 versionIf you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver, which will by necessity not be current

Monday, October 26, 2015

Updating to El Capitan and suddenly Maven is not working

I'm using IntelliJ IDE and when I updated to El Capitan version of OSX, I started getting this irritating pop out error in the IDE "can't start git: /usr/bin/git". I looked for a solution, and then I found this website: JetBrains Support

The solution is basically to install xcode (if you still don't have it) and agree to the licenses by running "sudo xcodebuild -license" in your terminal.

So, everything works well, I should be able to work properly the next day. And when I tried running my Selenium WebDriver scripts, my JAVA_HOME can't be found by Maven! So I need to search for a way to define my JAVA_HOME with minimal updates to the system (that's always the best way so you won't get confused when you want or need to update it).

I found this thread in stackoverflow.com and below is the solution that I did.

  1. In your home directory: (/Users/{username})
  2. nano .bash_profile and add the line below :
    export
    JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home`
  3. Make sure the path "/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home" exist.

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

Friday, February 6, 2015

Mac Terminal (Git / Maven / Path)

TO INSTALL AND CLONE A GIT REPO Download Git from http://git-scm.com/downloads Go to Terminal and type
$ git --version
There are times that you'll be asked to install the dev tools for command line, so just proceed with it. Clone Remote Git Repo git clone -b
$ git clone -b my-branch git@github.com:user/myproject.git
TO INSTALL MAVEN Download latest maven: http://maven.apache.org/download.cgi Follow the instructions under Unix-based OS (Linux, Solaris, and Mac OSX) If you want to go to /usr/local then you have to move the extracted maven folder using the terminal. Below are sample lines in moving the file
$ sudo mv -f /Users//apache-maven-3.2.5 /usr/local/
**replace to your actual user name, only if that's the location where you've extracted your maven; if not then just type the path to that location **I need to use sudo for the system to allow me to move the file to /usr/local Now I need to add apache maven bin to the paths. If I type "echo $PATH" the maven is still not included. I've edited PATH by
$ sudo nano /etc/paths
While editing the paths you'll find symbols like this ^X below, together with other options. This means Control+X (not Control+Shift+X). And for detailed explanation of java_home in mac, I find this website from apple developer really helpful, Apple Developer Q&A Java_Home. Once you've saved your paths, you should test if maven is properly installed by typing
$ mvn --version
if it does not work, try closing all your terminals and running it again. Then it should work, especially if maven path is showing already in PATH. Use "$ echo $PATH" to show all listed paths.

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