Tag Archives: Assertion

how to automate flash application with selenium webdriver? Example 3

 

Get the code

Link1

Link2

 

FlashObjectWebDriver.java

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;


public class FlashObjectWebDriver {
 private final WebDriver webDriver;
 private final String flashObjectId;

public FlashObjectWebDriver(final WebDriver webDriver, final String flashObjectId) {
 this.webDriver = webDriver;
 this.flashObjectId = flashObjectId;
 }

public String click(final String objectId, final String optionalButtonLabel) {
 return callFlashObject("doFlexClick", objectId, optionalButtonLabel);
 }

public String click(final String objectId) {
 return click(objectId, "");
 }

public String callFlashObject(final String functionName, final String... args) {
 final Object result =
 ((JavascriptExecutor)webDriver).executeScript(
 makeJsFunction(functionName, args),
 new Object[0]);

return result != null ? result.toString() : null;
 }

private String makeJsFunction(final String functionName, final String... args) {
 final StringBuffer functionArgs = new StringBuffer();

if (args.length > 0) {
 for (int i = 0; i < args.length; i++) {
 if (i > 0) {
 functionArgs.append(",");
 }
 functionArgs.append(String.format("'%1$s'", args[i]));
 }
 }
 return String.format(
 "return document.%1$s.%2$s(%3$s);",
 flashObjectId,
 functionName,
 functionArgs);
 }

}

 

Color_shawkath.java

import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;


public class Color_shawkath {


 private final static String GREEN = "GREEN";
 private final static String BLUE = "BLUE";
 private final static String RED = "RED";
 private final static String URL = "http://tinyurl.com/c937qt7";

 @Test
 public void TestColor() throws InterruptedException{
 FirefoxDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
 FlashObjectWebDriver flashApp = new FlashObjectWebDriver(driver, "coloredSquare");

 driver.get(URL);

 //Assert.assertEquals("Clicking Colors", driver.getTitle());

 Assert.assertEquals(GREEN, flashApp.callFlashObject("getColor"));
 flashApp.callFlashObject("click");
 Thread.sleep(10000L);
 Assert.assertEquals(BLUE, flashApp.callFlashObject("getColor"));
 flashApp.callFlashObject("click");
 Thread.sleep(10000L);
 Assert.assertEquals(RED, flashApp.callFlashObject("getColor"));
 flashApp.callFlashObject("click");
 Thread.sleep(10000L);
 Assert.assertEquals(GREEN, flashApp.callFlashObject("getColor"));

 driver.quit();
 }


}

WHAT IS DIFFERENCE BETWEEN ASSERT AND VERIFY ? – Selenium IDE

Please Refer this blog entry

Please Refer this blog entry

difference between assertion and verification

Please Refer this blog entry

Please Refer this blog entry

What is difference between assert and verify command in selenium??

Please Refer this blog entry

Please Refer this blog entry

Difefrence between Verify and Assert in Selenium

Please refer blog LINK

Assertion or Verification? – SELENIUM IDE

 

Effectively an “assert” will fail the test and abort the current test case,
whereas a “verify” will fail the test and continue to run the test case.

OR

Assert is best used when the check value has to pass for the test to be able to continue to run. Like a log in.

Verify is best used to check non critical things. Like hte presence of a headline element.

OR

dictionary meaning

Verification
Additional proof that something that was believed (some fact or hypothesis or theory) is correct

Assertion
A declaration that is made emphatically (as if no supporting evidence were necessary)

What @Rule in jUnit? What is ErrorCollector() in jUnit? How to use ErrorCollector()? Benefits of ErrorCollector()?

 

 

 

 

 

 


Annotation Type Rule
Annotates fields that contain rules. Such a field must be public, not static, and a subtype of TestRule. The Statement passed to the TestRule will run any Before methods, then the Test method, and finally any After methods, throwing an exception if any of these fail. If there are multiple annotated Rules on a class, they will be applied in an order that depends on your JVM’s implementation of the reflection API, which is undefined, in general. For example, here is a test class that creates a temporary folder before each test method, and deletes it after each:

Class ErrorCollector

The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect _all_ the incorrect rows in a table, and report them all at once):

Example 1:

package myWorkjUnit;

import junit.framework.Assert;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class LearnAssertions {

/*
 * The ErrorCollector Rule allows execution of a test to continue after the
 * first problem is found and report them all at once
 */
 @Rule
 public ErrorCollector errCol = new ErrorCollector();

 @Test
 public void testAdvertisingPrograms(){
 System.out.println("open browser and go to URL");
 System.out.println("click on link");
 System.out.println("click on one Login link");

 String actual = "actual";
 String expected = "Expected";
// if("ElementLocation" == "ElementLocation from abve statement"){
// System.out.println("Pass");
// }else{
// System.out.println("fail");
// report Error
// }

 System.out.println("before Assert.assertEquals(expected, actual)");

 try {
 Assert.assertEquals(expected, actual);
 } catch (Throwable e) {
 System.out.println("Report Error" + e);
 errCol.addError(e);
 }

 System.out.println("After Assert.assertEquals(expected, actual)");

 }

}

&nbsp;

Example 2: Lets collect more errors ……………

package myWorkjUnit;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class UsesErrorCollectorTwice {
 /*
 * The ErrorCollector Rule allows execution of a test to continue after the
 * first problem is found and report them all at once
 */
 @Rule
 public ErrorCollector collector= new ErrorCollector();

@Test
 public void example() {
 collector.addError(new Throwable("first thing went wrong"));
 collector.addError(new Throwable("second thing went wrong"));

 // all lines will run, and then a combined failure logged at the end.
 }

}

ErrorCollector

ErrorCollector

References:

Rule (JUnit API)

ErrorCollector (JUnit API)

catch and handle assertion errors

Lets see what happens with lines after assertion statement

Assert.assertEquals(expected, actual);

 

package myWorkjUnit;

import junit.framework.Assert;

import org.junit.Test;

public class LearnAssertions {

@Test
 public void testAdvertisingPrograms(){
 System.out.println("open browser and go to URL");
 System.out.println("click on link");
 System.out.println("click on one Login link");

 String actual = "actual";
 String expected = "Expected";
// if("ElementLocation" == "ElementLocation from abve statement"){
// System.out.println("Pass");
// }else{
// System.out.println("fail");
// report Error
// }

 System.out.println("before Assert.assertEquals(expected, actual)");
 Assert.assertEquals(expected, actual);
 System.out.println("After Assert.assertEquals(expected, actual)");



 }

}

 

check out eclipse console after running above code using junit

 

following line of code is not executed because of assertion

System.out.println(“After Assert.assertEquals(expected, actual)”);

 

Lets handle this

We can use Try Catch block to handle assertion and catch error and continue further program execution.

package myWorkjUnit;

import junit.framework.Assert;

import org.junit.Test;

public class LearnAssertions {

@Test
 public void testAdvertisingPrograms(){
 System.out.println("open browser and go to URL");
 System.out.println("click on link");
 System.out.println("click on one Login link");

 String actual = "actual";
 String expected = "Expected";
// if("ElementLocation" == "ElementLocation from abve statement"){
// System.out.println("Pass");
// }else{
// System.out.println("fail");
// report Error
// }

 System.out.println("before Assert.assertEquals(expected, actual)");

 try {
 Assert.assertEquals(expected, actual);
 } catch (Throwable e) {
 System.out.println("Report Error" + e);
 }

 System.out.println("After Assert.assertEquals(expected, actual)");


 }

}

 

Lets check out console

 

Smart one 🙂

how to use assertions in junit ? part 1

What is assertion?

 

An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.

Experience has shown that writing assertions while programming is one of the quickest and most effective ways to detect and correct bugs. As an added benefit, assertions serve to document the inner workings of your program, enhancing maintainability.

 

Lets have a look for simple Example with jUnit:

Condition : expected = actual

LearnAssertions.java

package myWorkjUnit;

import junit.framework.Assert;

import org.junit.Test;

public class LearnAssertions {

@Test
 public void testAdvertisingPrograms(){
 System.out.println("open browser and go to URL");
 System.out.println("click on link");
 System.out.println("click on one Login link");

 String actual = "actual";
 String expected = "actual";

// if("ElementLocation" == "ElementLocation from above statement"){
// System.out.println("Pass");
// }else{
// System.out.println("fail");
// report Error
// }


 Assert.assertEquals(expected, actual);

 }

}

Condition : expected != actual

package myWorkjUnit;

import junit.framework.Assert;

import org.junit.Test;

public class LearnAssertions {

@Test
 public void testAdvertisingPrograms(){
 System.out.println("open browser and go to URL");
 System.out.println("click on link");
 System.out.println("click on one Login link");

 String actual = "actual";
 String expected = "expected";

// if("ElementLocation" == "ElementLocation from above statement"){
// System.out.println("Pass");
// }else{
// System.out.println("fail");
// report Error
// }


 Assert.assertEquals(expected, actual);

 }

}

how to use assertion in testng ? – Assert (TestNG)

Refer Assert (TestNG) 

 

 

Sample Method Detail

assertTrue
assertFalse
assertEquals

 

Example 1 : what happen when assertion fail using  assertEquals ?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertEquals(50, multiply(10, 5));
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}

 

Run above code using testNG and check TestNG result pane

assertEquals

assertEquals

 

 

Example 2 : what happen when assertion success using  assertEquals ?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertEquals(2, multiply(10, 5));
 }


 public int multiply(int x, int y) {
 return x / y;
 }
}

Condition when success

Condition when success

Example 3: what happen when assertion success using  assertTrue?

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertTrue(multiply(10, 5)!=20, "Sorry");
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}

 

what happen when assertion success using  assertTrue

what happen when assertion success using assertTrue

 

 

Example 4: what happen when assertion fail using  assertTrue?

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertTrue(multiply(10, 5)!=2, "Sorry");
 }


 public int multiply(int x, int y) {
 return x / y;
 }

}
what happen when assertion fail using  assertTrue

what happen when assertion fail using assertTrue

 

Example 4: what happen when assertion success using  assertFalse?

 

package testWordpress;

import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertionsTest {

 @Test
 public void testMultiply() {

 Assert.assertFalse(multiply(10, 5)>10, "Caught");
 }


 public int multiply(int x, int y) {
 return x / y;
 }
}

what happen when assertion success using  assertFalse

what happen when assertion success using assertFalse

 

Lets add TestCase with assertion in testng.xml and verify testNG html report

 

<test name="test4">
 <classes>
 <class name="testWordpress.AssertionsTest" ></class>
 </classes>
</test>

 

 

Pupunzi

For a better web

Tech

News and reviews from the world of gadgets, gear, apps and the web

VentureBeat

News About Tech, Money and Innovation

Digital Inspiration

Tips and tutorials around all things tech.

Thinkwareglobal's Blog

Just another WordPress.com weblog

Kenazari's Blog

Just another WordPress.com weblog

Richfowler1's Blog

Just another WordPress.com weblog

Romaicus's Blog

Just another WordPress.com weblog

botlaguduri

Just another WordPress.com site

chandanag89

Just another WordPress.com site

meghshetty

Just another WordPress.com site

digitalmediaexpert

digital media expert blog

Thinking in Silverlight

Silverlight/WPF/Windows 8 Store App

Different Journeys

Let's make life easier!

LAW RESOURCE INDIA

LEGAL RESOURCE CENTRE / COURT JUDGMENTS / LEGAL ARCHIVES

BookConnect

Promoting Indian language Books

Actively Lazy

Software, crafted with passion

SAPonPower

An ongoing discussion about SAP infrastructure

SAP Career

Your Career Mentor

hrsapcertification

Just another WordPress.com site

The secret product manager

Thoughts about product management, SUSE, Linux, ARM processors, Cloud, AIX, Power Systems, and other things that interest me

Sap Certification Material . Com

Quality store for sap software and courses

Simon Les Flex's Blog

SAP Process Integration Specialist - NOW WE CAN DO BETTER!

Oracle Technologies Primer

An Oracle Fusion Middleware and iPaaS blog!

Gilberto Holms

Java, Middleware, SOA Architecture Blog

The Skeleton

Java code skeletons, tips and pointers

J@n van Zoggel

integration, innovation & random technology stuff

The Pragmatic Integrator

Hints and tips from a pragmatic integration specialist

briskwalk

The pace of life..

Optimalbg's Blog

Just another WordPress.com site

IT Jobs in USA

Keep the track of this Blog if you are looking out for some good Opportunities to work in USA....

Referral Jobs | Right Place to Get a Job

Hurry you are one Click away to get a job - Fresher Jobs for 2012, 2013 fresher’s and also high paid jobs for well Experienced IT professionals on Referal jobs.

eccinternational.wordpress.com/

ISO Consulting | ISO 27001 | ISO 20000 | CMMI | BCM | GRI Sustainability Reporting & Assurance | eLearning | Trainings | Philippines

Michael Korn's Blog

Musings on Life, Career, Faith and Technology

WORKINGWITHQTP

Just another WordPress.com weblog

Solutions365

.net, WCF, Unity, IOC, SQL

SQA and Testers Blog

Just another WordPress.com weblog

Expertqtp's Weblog

Just another WordPress.com weblog

Sai Chamarthi

Experienced Testing Professional

saxenavinay

A great WordPress.com site

Suresh4qtp's Blog

Just another WordPress.com site

jonah95hill

This WordPress.com site is the cat’s pajamas

It's always something...

Just another WordPress.com weblog

Faculty of Information Technology - Lê Ngọc Tiến

Be Yourself! Simplify Our Goals! Nothing is Impossible!

Anti-Malware Testing

Thoughts on security product testing past and present

Methods & Tools

Practical knowledge for the software developer, tester and project manager