Event Driven Selenium Automation Program

package automation; //Your package name in our case “automation”

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import java.awt.*;

import java.awt.event.*;

import java.util.concurrent.TimeUnit;

import javax.swing.*; v

//Your class which implements ActionListener in our case “SmokeTest” class.

public class SmokeTest implements ActionListener {

JFrame myFrame ;

JLabel lb;

JPanel txt;

//Method used for setting layout of GUI

public void initGUI ( ) {

//Step 2: setup the top level container

myFrame = new JFrame();

//Step 3: Get the component area of top-level

Container c = myFrame.getContentPane();

//Step 4: Apply layouts

c.setLayout( new FlowLayout( ) );

//Step 5: create & add components

JTextField tf = new JTextField(23);

JButton b1 = new JButton(“Start Testing”); v

JLabel lb = new JLabel(“https://Your web site URL “);

txt = new JPanel (new GridLayout(4,4));

txt.add(tf);

txt.add(b1);

c.add(b1, BorderLayout.CENTER);

c.add(tf, BorderLayout.SOUTH);

c.add(txt, BorderLayout.SOUTH);

String s=lb.getText();

tf.setText(s);

tf.setEditable(false);

b1.addActionListener(this);

//Step 6: set size of frame and make it visible

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setSize(500,500);

myFrame.setVisible(true);

}//end initGUI method

public SmokeTest () { // default constructor

initGUI ();

}

public void actionPerformed(ActionEvent event) {

JOptionPane.showMessageDialog(null,”Smoke Test is started now, Please wait…”);

String exePath = “C:\\Users\\Arshad\\Desktop\\New folder (2)\\chromedriver.exe”;

System.setProperty(“webdriver.chrome.driver”, exePath);

WebDriver driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.navigate().to(“https://Your web site URL “);

//Showing the opened success message

String LoadMsg=”URL is accessible of web site “;

JOptionPane.showMessageDialog(null,LoadMsg);

//System.out.println(LoadMsg);

// Entering the User Name

WebElement UserName = driver.findElement(By.name(“element name “));

UserName.sendKeys(“UserName”);

// Entering the Password

WebElement PassWord = driver.findElement(By.name(“web element name”));

PassWord.sendKeys(“Password”);

// Clicking on login button

WebElement login = driver.findElement(By.id(“login button id”));

login.click();

//Showing the Login success message

String LoginMsg=”Successfully login the web site “;

JOptionPane.showMessageDialog(null,LoginMsg);

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

// Now moving towards the menu page

WebElement TopMenu = driver.findElement(By.className(“CSS class name”));

TopMenu.click();

//<>>>>>>>>>>>>>>>>>>>>>(1st Page)>>>>>>>>>>>>>>>>>>>>>>>>

//Navigating to your page

driver.navigate().to(“https://Page URL”);

String j = driver.getTitle();

String k = “Your page title”;

if( j.equals(k)){

//Showing the success message

String UsersReportMsg=”Successfully Open the page “;

JOptionPane.showMessageDialog(null,UsersReportMsg);

//System.out.println(UsersReportMsg);

}

else

JOptionPane.showMessageDialog(null,j);

// Note: Your can repeat the obove step for your other pages to check whether they are accessible or not

//Log off the application

WebElement LogOffMenu = driver.findElement(By.className(“CSS Class Name”));

LogOffMenu.click();

driver.navigate().to(“https:// Your log off page URL “);

}

// Main method

public static void main(String[] args) {

SmokeTest gui = new SmokeTest();

}

} // End of class

// Java code was executed in Eclipse, Windows 7 32 bit

//Eclipse IDE for Java Developers

//Version: Luna Service Release 2 (4.4.2)

//Build id: 20150219-0600

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: