Thursday 29 January 2015

How to setup or run automation scripts on chrome browser for Selenium automation testing


How to setup or run automation scripts on chrome browser for Selenium automation testing 

Here I am giving some example to control browsers from config.properties file as well.

Step 1:Create a folder name config under your project 
Step 2: Create a file name call it as config.properties file

Step 3: paste this  
#### Control browsers from here 'Firefox, Chrome, Internet Explorer’ whenever you want to change browser you can change here directly.
BROWSER=chrome
Step 4: 
Download Chrome driver from this page


or   directly from here

Create a folder in your selenium project as Chrome driver and place this Chrome  .exe file in that folder

And now right click on that .exe take path.

Example path like this :     projectname//src//chromedriver//chromedriver



Step 5: Create a java file as below


import java.io.FileInputStream;
import java.io.InputStream;
import java.util.PropertyResourceBundle;

import java.util.ResourceBundle;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverStart 
{
public static WebDriver driver;
public static String BROWSER = null;
public static String USERDIR = System.getProperty("user.dir”);
public static ResourceBundle bundle = null;
public static InputStream initPropertyFile = null;
public static String PROPERTY_PATH = null;
public JavascriptExecutor jse;


PROPERTY_PATH = USERDIR
+ "/Config/config.properties";
if (initPropertyFile == null) {
initPropertyFile = new FileInputStream(PROPERTY_PATH);  
}
if (bundle == null) {
bundle = new PropertyResourceBundle(initPropertyFile);
}

BROWSER = bundle.getString("BROWSER");


if (BROWSER.equalsIgnoreCase("chrome")) {
File file = new File(“Chrome drive .exe file path");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
builder = new Actions(driver); 
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );

                                       jse = (JavascriptExecutor) driver;

 }


// Here you can call Firefox..
if (BROWSER.equalsIgnoreCase("firefox")) {

driver = new FirefoxDriver();
builder = new Actions(driver); 
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );
jse = (JavascriptExecutor) driver;

}

}



Tuesday 27 January 2015

How to automate scroll up or down button in selenium java

How to automate scroll up or down button in selenium java


WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
or, you can do as follows:

jse.executeScript("scroll(0, 250);”);

If you want to do up use this script.
 For scroll up: jse.executeScript("scroll(250, 0)");

Thursday 22 January 2015

How to create a dynamic email id in selenium cucumber java in automation testing


How to create a dynamic email id in selenium cucumber java in automation testing

public static final DateFormat EMAIL_ DATE_FORMAT = new SimpleDateFormat("HHmmss-dd-MMM-yyyy”);
This method you can create in superclass that class name is Utils
  public static String getUniqueEmailName() {
                    return EMAIL_ DATE_FORMAT.format(new Date()) + “@hotmail.com";
    }

And then in sub class you can call like this… 
public void Click(String Value, Utils utils) throws Exception {

public static String genEmailEdited;
genEmailEdited = superclass or utils.getUniqueEmailName();
System.out.println("the new edited email is --------->" + genEmailEdited);

driver.findElement(By.xpath(xpathcomehere )).sendKeys(genEmailEdited);
}
OR


import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] argv) throws Exception {
    Format formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
    String today = formatter.format(new Date());
    System.out.println("Today : " + today);

  }
}

Friday 16 January 2015

How to verify whether the image is present or not on Selenium java cucubmer

How to verify whether the image is present or not on Selenium java cucubmer

if(driver.findElement(By.xpath("xpath of
image")).getAttribute("src").contains("image file name present in the html
source"));

it just verifies that the given image link present in the html source or
not. To verify that whether the image is displayed or not, you try to use
isDisplayed() command.

Wednesday 7 January 2015

java.lang.ClassNotFoundException: com.google.common.net.MediaType



Solution

You need Google Guava in your classpath by the looks of things. This is where the class com.google.common.base.Function lives.

Just in case if you are using maven setup include this dependency in pom.xml

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>



Error looks as below:





java.lang.NoClassDefFoundError: com/google/common/net/MediaType
at org.openqa.selenium.remote.http.JsonHttpCommandCodec.encode(JsonHttpCommandCodec.java:197)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:171)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:160)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
at skeleton.Utils.init(Utils.java:127)
at skeleton.Stepdefs.i_wait_hour(Stepdefs.java:27)
at ?.When I wait 1 hour(skeleton/belly.feature:5)
Caused by: java.lang.ClassNotFoundException: com.google.common.net.MediaType
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.openqa.selenium.remote.http.JsonHttpCommandCodec.encode(JsonHttpCommandCodec.java:197)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:171)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:160)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
at skeleton.Utils.init(Utils.java:127)
at skeleton.Stepdefs.i_wait_hour(Stepdefs.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at cucumber.runtime.Utils$1.call(Utils.java:34)
at cucumber.runtime.Timeout.timeout(Timeout.java:13)
at cucumber.runtime.Utils.invoke(Utils.java:30)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:35)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:298)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:48)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

Monday 5 January 2015

BDD cucumber example

BDD cucumber example
http://aslakhellesoy.com/post/20006051268/cucumber-jvm-1-0-0

https://github.com/cucumber/cucumber-java-skeleton

https://github.com/cucumber/cucumber-java-skeleton/releases

The best explanation

http://www.srccodes.com/p/article/48/cucumber-test-behavior-driven-development-bdd-feature-step-runner-glue-gherkin-data-table-scenario-given-when-then

BDD JVM Cucumber with masterthoughts reporting pom.xml workable

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.stag.simplecucumber</groupId>
    <artifactId>simple-cucumber-jvm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <cucumber-jvm.version>1.0.11</cucumber-jvm.version>
        <selenium.version>2.44.0</selenium.version>
        <junit.version>4.11</junit.version>
        <picocontainer.version>2.13.6</picocontainer.version>
        <cucumber-reporting.version>0.0.17</cucumber-reporting.version>
    </properties>
    
    
    <repositories>
       <repository>
           <id>repo.bodar.com</id>
           <url>http://repo.bodar.com</url>
       </repository>
       <repository>
                <id>sonatype-releases</id>
                <url>https://oss.sonatype.org/content/repositories/releases/</url>
            </repository>
   </repositories>

  
    <dependencies>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>${cucumber-jvm.version}</version>
            <scope>test</scope>
        </dependency>
        
         <dependency>
           <groupId>com.googlecode.totallylazy</groupId>
           <artifactId>totallylazy</artifactId>
           <version>1077</version>
   </dependency>
         
             <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
   <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.picocontainer</groupId>
            <artifactId>picocontainer</artifactId>
            <version>${picocontainer.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
      <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
      
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>${cucumber-reporting.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>0.0.5</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>cucumber-jvm-example</projectName>
                            <outputDirectory>${project.build.directory}/cucumber-html-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
                
                            <enableFlashCharts>false</enableFlashCharts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
             
        </plugins>
    </build>


</project>

org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description

org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description

I found the cause of this the problem is that the cucumber.json file does not exist.

I addressed this in my project by adding json:target under the @CucumberOptions annotation, this then generated the json. for example @CucumberOptions(features = "src/test/resources/features/some.feature", monochrome = false, format = {"pretty", "json:target/cucumber.json"})

The masterthought plugin then processes the generated JSON by looking for it at ${project.build.directory}/cucumber.json

At this point the Maven plugin generates the HTML from the JSON.


For this you have to setup as below at both the places

and in Junit runner .. mention "json:target/cucumber.json" problem will resolve.


package skeleton;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(format = {
        "pretty","html:target/cucumber-html-reports","json:target/cucumber.json" })


public class RunCukesTest {
}



In pom.xml do like this
 <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>0.0.5</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>cucumber-jvm-example</projectName>
                            <outputDirectory>${project.build.directory}/cucumber-html-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
                
                            <enableFlashCharts>false</enableFlashCharts>
                        </configuration>
                    </execution>
                </executions>

            </plugin>


References

https://github.com/masterthought/maven-cucumber-reporting-mojo

http://www.masterthought.net/section/cucumber-reporting-downloads