· 6 years ago · Aug 29, 2019, 08:38 PM
1
2Practical 1
3
4Aim: Install Selenium IDE: write a test suite containing minimum 4 test cases for different formats.
5
6Description:
7
8Installation of Selenium IDE
91. Launch Mozilla Firefox Browser
102. Type URL https://www.seleniumhq.org/download/
11OR
123. https://addons.mozilla.org/en-US/firefox/addon/selenium-ide
134. Selenium IDE Add-ons page will get open then Click on Add to Firefox button
145. Firefox will show one popup saying do you want to allow Mozilla Firefox to install Selenium IDE Add-ons or not. Click on Install button.
156. Firefox will automatically install Selenium IDE software. After the installation is completed, a pop up window appears asking to re-start the Firefox. Click on the "Restart Now" button to reflect the Selenium IDE installation.Click on Restart Now button
167. On clicking on the Restart Now button, Firefox will restart automatically. In case you missed the pop-up, simply close the Firefox and launch again.
178. Once the Firefox booted and started again, we can see selenium IDE under the tools menu list. Selenium IDEicon will be displayed in the Firefox toolbar
18
19Testing Websites
201. Launch Selenium IDE, after launching a pop up appears like shown below, select “Record a new test in a new project”
21
222. Enter name of the project, click on “OK”
23
243. Enter Base URL for your project, and click on “START RECORDING”
25
264. Once you click on “START RECORDING” browser will open the Base URL you supplied( “https://www.google.com”). Record your operations over the specified website once you have performed all the operations then open Selenium IDE window and click on stop recording icon
27
28
295. Once you click on stop recording icon a pop as shown below will ask for test name. Provide test name and click on “OK”
30
316. Once saved you can run your test again. To run the test again click on “Run Current Test” icon
32
33your test will run in the browser
347. Repeat the above steps with three more websites
35a. https://www.facebook.com
36b. https://www.twitter.com
37c. https://www.instagram.com
38
39Output:
40
41
42Practical 2
43
44Aim: Conduct a test suite for any two websites.
45
46Description:
47
48Steps to Create Test Case:
491. Open (Example: Type www.google.com)
502. Type "energy efficient" in the Google Search Input Box
513. Right Click outside on an empty spot
524. Select Selenium IDE
53
545. Verify the Text Present as "energy efficient"
55
566. Assert the Title
57
587. Stop the recording
598. Run the test suite, To run the test again click on “Run Current Test” icon
60
619. Repeat the above process with another search engine, for example, www.bing.com
62
63Output:
64
65
66Practical 3
67
68
69Aim: Install Selenium server and demonstrate it using a script in Java/PHP.
70
71Description:
72
731. Download Selenium server: http:/scleniumhq.org/download/
742. Download Selenium Client driver for Java (from Selenium Client Drivers section)
753. Open Chrome and type “java jdk” in search bar
764. Click on Java SE - Downloads | Oracle Technology Network >> Download >> Accept license for JDK 12.0.2 >> jdk-12.0.2_windows-x64_bin.exe
775. Open File Explorer and open the following folder C:\jdk-12.0.2\bin and copy the address from address bar
786. Open Control Panel Click on System and Security >> System >> Advanced System Settings >> Environmental Variables >> System Variables >> Path >> Edit >> New >> Paste the copied address >> OK >> OK >> OK
797. Open cmd and execute following commands
80a. java -version
81b. javac
828. Open Chrome and type “Eclipse” in search bar
839. Click on Eclipse Downloads | The Eclipse Foundation >> Download 64-bit >> Download
8410. Once downloaded, open Eclipes Installer. Click on icon >> Update >> Accept >> OK
8511. Select Eclipse IDE for Java Developers >> Install >> Accept now >> Accept >> Select All >> Accept >> Launch. This should open a welcome screen.
8612. Close welcome screen tab and Click on File >> New >> Java Project >> Name your project >> Finish.
8713. Once you create a Java project a Package Explorer should open on Left side of the window. Click on src >> New >> Class >> Provide a class name >> check public static void main(String [] args) >> Finish.
8814. Once you create a java class type the source code and run application
89
90Source Code:
91
92import java.io.BufferedReader;
93import java.io.IOException;
94import java.io.InputStreamReader;
95
96public class operation {
97 public static void main(String[] args) throws IOException {
98 int a, b, c;
99 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
100 System.out.println("Enter a number: ");
101 a = Integer.parseInt(br.readLine());
102 System.out.println("Enter a number: ");
103 b = Integer.parseInt(br.readLine());
104 c = a + b;
105 System.out.println("Sum of two numbers: " + c);
106 }
107}
108
109Output:
110
111
112
113Practical 4
114
115
116Aim: Write and test a program to login a specific web page.
117
118Description:
119
120Installing Window Builder in Eclipse
1211. Open Eclipse IDE
1222. Go to Help menu
1233. Select Install New Software >> Click on Drop Down List >> Select http://download.eclipse.org/releases/long >> Select “General Purpose Tools” >> Select the packages from Swing Designer upto Windows Builder >> Click on “Next” >> Click on “Next” >> Select license agreement >> Click “Finish”.
1244. Then it will ask you to restart Eclipse Click on “Yes”
125
126Steps to Create a Login page
1271. Open Eclipse IDE
1282. Go to File menu >> New >> Java Project >> Name your project >> Next >> Finish
1293. Once you create a Java project a Package Explorer should open on Left side of the window. Click on src >> New >> others >> Windows Builder >> Swing Designer >> Application Window >> Next >> Name Application Window >> Finish.
1304. Go to Design view. Select Absolute layout and drop it on the Frame >> Select JLabel and drop it on Frame >> Change its text property to Username >> Select JLabel and drop it on Frame >> Change its text property to Password >> Select JTextField and drop it on frame >> Change its Variable Name to uname >> Select JTextField and drop it on frame >> Change its Variable Name to pass
1315. Now Select a JButton and drop it on Frame >> Change its text property to Login >> Right Click on JButton >> Add event handler >> action >> actionPerformed
1326. Type the source code and Run the application.
133
134Source Code:
135
136public void actionPerformed(ActionEvent arg0)
137{
138 String uname= textField.getText();
139 String pwd=passwordField.getText();
140 if(uname.equals("Username") && pwd.equals("Password"))
141 JOptionPane.showMessageDialog(frame, "You are Logged in");
142 else
143 JOptionPane.showMessageDialog(frame, "Invalid Username or Password");
144 }
145
146Output:
147
148
149
150
151
152
153
154
155
156Practical 5
157
158
159Aim: Write and test program to update 5 students records into table into Excel file.
160
161Description:
162
163JXL – Java Excel API
164JXL API (a.k.a. Java Excel API) allows users to read, write, create, and modify sheets in an Excel(.xls) workbook at runtime. It doesn't support .xlsx format.
165JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls.
166JXL API is widely used with Selenium.
167
168jxl.write – This package includes all the interfaces and classes that allows user to wirte or modify data in excel sheets.
169Interface - WriteableSheet
170Class - Number
171
172
173public interface Workbook extends java.io.Closeable, java.lang.Iterable<Sheet> - High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets/etc.
174Sheet createSheet(java.lang.String sheetname)
175Create a new sheet for this Workbook and return the high level representation. Use this to create new sheets.
176Excel allows sheet names up to 31 chars in length but other applications (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars, others - truncate such names to 31 character.
177
178public interface WritableSheet extends Sheet - Interface for a worksheet that may be modified. The most important modification for a sheet is to have cells added to it.
179addCell(WriteableCell cell) - Adds a cell to this sheet The RowsExceededException may be caught if client code wishes to explicitly trap the case where too many rows have been written to the current sheet.
180
181Source Code:
182
183import jxl.Workbook;
184import jxl.write.*;
185import jxl.write.Number;
186//import org.openqa.selenium.remote.html5.AddLocationContext;
187import java.io.File;
188import java.io.IOException;
189
190public class webclass {
191 private static final String EXCEL_FILE_LOCATION = "D:\\StudentsRecord.xls";
192 public static void main(String[] args) {
193
194 String names[]={"Ram","Jai","Shyam","Raj","Rahul"};
195 int marks[]={60,70,50,65,80};
196 //1. Create an Excel file
197 WritableWorkbook myFirstWbook = null;
198 try {
199
200 myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));
201
202 // create an Excel sheet
203 WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);
204
205 // add something into the Excel sheet
206 Label label = new Label(0, 0, "Roll. No.");
207 excelSheet.addCell(label);
208
209 for(int i=0;i<5;i++)
210 {
211 Number number = new Number(0, i+1, i+1);
212 excelSheet.addCell(number);
213 }
214
215 label = new Label(1, 0, "Student Name");
216 excelSheet.addCell(label);
217
218 for(int i=0;i<5;i++)
219 {
220 Label name = new Label(1, i+1, names[i]);
221 excelSheet.addCell(name);
222 }
223
224 label = new Label(2, 0, "Marks");
225 excelSheet.addCell(label);
226
227 for(int i=0;i<5;i++)
228 {
229 Number number = new Number(2, i+1,marks[i]);
230 excelSheet.addCell(number);
231 }
232 myFirstWbook.write();
233 } catch (IOException e) {
234 e.printStackTrace();
235 } catch (WriteException e) {
236 e.printStackTrace();
237 } finally {
238
239 if (myFirstWbook != null) {
240 try {
241 myFirstWbook.close();
242 } catch (IOException e) {
243 e.printStackTrace();
244 } catch (WriteException e) {
245 e.printStackTrace();
246 }
247 }
248 }
249 }
250}
251
252Output:
253
254Console Output:
255
256
257Excel Sheet Created:
258
259
260Practical 6
261
262
263Aim: Write and test a program to read data from an Excel file.
264
265Description:
266
267JXL – Java Excel API
268JXL API (a.k.a. Java Excel API) allows users to read, write, create, and modify sheets in an Excel(.xls) workbook at runtime. It doesn't support .xlsx format.
269JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls.
270JXL API is widely used with Selenium.
271
272public interface Workbook extends java.io.Closeable, java.lang.Iterable<Sheet> - High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets/etc.
273getWorkbook(java.io.File file) - A factory method which takes in an excel file and reads in the contents.
274getSheet(int index) - Gets the specified sheet within this workbook As described in the accompanying technical notes, each call to getSheet forces a reread of the sheet (for memory reasons).
275
276public interface Cell - Represents an individual Cell within a Sheet. May be queried for its type and its content
277public java.lang.String getContents() - Quick and dirty function to return the contents of this cell as a string. For more complex manipulation of the contents, it is necessary to cast this interface to correct subinterface. Returns the contents of this cell as a string
278
279public interface Sheet - Represents a sheet within a workbook. Provides a handle to the individual cells, or lines of cells (grouped by Row or Column)
280public Cell getCell(int column, int row) - Returns the cell specified at this row and at this column. If a column/row combination forms part of a merged group of cells then (unless it is the first cell of the group) a blank cell will be returned
281Parameters:
282column - the column number
283row - the row number
284Returns: the cell at the specified co-ordinates
285
286Source Code:
287
288import jxl.Cell;
289import jxl.Sheet;
290import jxl.Workbook;
291import jxl.read.biff.BiffException;
292import java.io.File;
293import java.io.IOException;
294
295public class readfile {
296
297 private static final String EXCEL_FILE_LOCATION = "D:\\StudentsRecord.xls";
298
299 public static void main(String[] args) {
300 Workbook workbook = null;
301 try {
302 workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION));
303 Sheet sheet = workbook.getSheet(0);
304 Cell cell1 = sheet.getCell(0, 0);
305 Cell cell2 = sheet.getCell(1, 0);
306 Cell cell3 = sheet.getCell(2, 0);
307 for(int i=0;i<5;i++)
308 {
309 Cell cell = sheet.getCell(0, i+1);
310 System.out.println(cell1.getContents()+": "+cell.getContents());
311 Cell celln = sheet.getCell(1, i+1);
312 System.out.println(cell2.getContents()+": "+celln.getContents());
313 Cell cellm = sheet.getCell(2, i+1);
314 System.out.println(cell3.getContents()+": "+cellm.getContents()+"\n");
315 }
316 } catch (IOException e) {
317 e.printStackTrace();
318 } catch (BiffException e) {
319 e.printStackTrace();
320 } finally {
321 if (workbook != null) {
322 workbook.close();
323 }
324 }
325 }
326}
327
328Output:
329
330Data Present in Excel Sheet:
331
332
333Console Output:
334
335
336Practical 7
337
338
339Aim: Write a program to count the number of objects present/available on the page.
340
341Description:
342
343org.openqa.selenium
344public abstract class By extends Object - Mechanism used to locate elements within a document. In order to create your own locating mechanisms, it is possible to subclass this class and override the protected methods as required, though it is expected that all subclasses rely on the basic finding mechanisms provided through static methods of this class.
345public static By tagName(java.lang.String tagName)
346Parameters: tagName - The element's tag name.
347Returns: A By which locates elements by their tag name.
348
349public interface WebDriver extends SearchContext - The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:
350• Control of the browser itself
351• Selection of WebElements
352• Debugging aids
353java.util.List<WebElement> findElements(By by) - Find all elements within the current page using the given mechanism. This method is affected by the 'implicit wait' times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.
354Parameters: by - The locating mechanism to use
355Returns: A list of all WebElements, or an empty list if nothing matches
356
357void get(java.lang.String url) - Load a new web page in the current browser window.
358Parameters: url - The URL to load. It is best to use a fully qualified URL
359
360WebDriver.Options manage() - Gets the Option interface
361Returns: An option interface
362
363WebDriver.Window window() - Returns the interface for managing the current window.
364
365public interface WebElement extends SearchContext, TakesScreenshot - Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.
366
367org.openqa.selenium.firefox
368public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
369The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
370
371public static String setProperty(String key, String value ) - The setProperty() method of Java system class sets the property of the system which is indicated by a key.
372Parameters:
373key - It is the name of the system property.
374value - It is the value of the system property.
375
376Source Code:
377
378file.html
379<!DOCTYPE HTML>
380<html>
381 <body>
382 <h2>To check the links present in web page</h2>
383 <a href="https://www.google.com">Google</a><br>
384 <a href="https://www.youtube.com">Youtube</a><br>
385 <a href="https://www.facebook.com">facebook</a><br>
386 <a href="https://www.gmail.com">Gmail</a><br>
387 </body>
388</html>
389
390links.java
391import org.openqa.selenium.By;
392import org.openqa.selenium.WebDriver;
393import org.openqa.selenium.WebElement;
394import org.openqa.selenium.firefox.FirefoxDriver;
395public class links {
396 public static void main(String[] args)
397 {
398 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
399 WebDriver driver = new FirefoxDriver();
400 driver.get("file:///C:/Users/VRUTIKA/Desktop/links.html");
401 driver.manage().window().maximize();
402 java.util.List<WebElement> links=driver.findElements(By.tagName("a"));
403 System.out.println("total no of links are: "+links.size());
404 System.out.println("the name of the links are: ");
405 for(int i=0;i<links.size();i++)
406 {
407 System.out.println("link: "+(i+1)+" link name: "+links.get(i).getText());
408 }
409 }
410}
411
412Output:
413
414Output for file.html:
415
416
417Output for links.java:
418
419
420Practical 8
421
422
423Aim: Write a program to get the number of items from a list box/combobox from a webpage.
424
425Description:
426
427org.openqa.selenium
428public abstract class By extends Object - Mechanism used to locate elements within a document. In order to create your own locating mechanisms, it is possible to subclass this class and override the protected methods as required, though it is expected that all subclasses rely on the basic finding mechanisms provided through static methods of this class.
429public static By tagName(java.lang.String tagName)
430Parameters: tagName - The element's tag name.
431Returns: A By which locates elements by their tag name.
432
433public interface WebDriver extends SearchContext - The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:
434• Control of the browser itself
435• Selection of WebElements
436• Debugging aids
437java.util.List<WebElement> findElements(By by) - Find all elements within the current page using the given mechanism. This method is affected by the 'implicit wait' times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.
438Parameters: by - The locating mechanism to use
439Returns: A list of all WebElements, or an empty list if nothing matches
440
441void get(java.lang.String url) - Load a new web page in the current browser window.
442Parameters: url - The URL to load. It is best to use a fully qualified URL
443
444WebDriver.Options manage() - Gets the Option interface
445Returns: An option interface
446
447WebDriver.Window window() - Returns the interface for managing the current window.
448
449public interface WebElement extends SearchContext, TakesScreenshot - Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.
450
451org.openqa.selenium.firefox
452public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
453The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
454
455public static String setProperty(String key, String value ) - The setProperty() method of Java system class sets the property of the system which is indicated by a key.
456Parameters:
457key - It is the name of the system property.
458value - It is the value of the system property.
459
460Source Code:
461
462listbox.html
463<!DOCTYPE HTML>
464<html>
465 <body>
466 <h2>To check the items of combo-box present in the web page</h2>
467 <select name="menu">
468 <option value="0" selected>zero</+option>
469 <option value="1">one</option>
470 <option value="2">two</option>
471 <option value="3">three</option>
472 <option value="others">four</option>
473 </select>
474 <br>
475 </body>
476</html>
477
478listbox.java
479import org.openqa.selenium.By;
480import org.openqa.selenium.WebDriver;
481import org.openqa.selenium.WebElement;
482import org.openqa.selenium.firefox.FirefoxDriver;
483public class listbox {
484
485 public static void main(String[] args) {
486 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
487 WebDriver driver = new FirefoxDriver();
488 driver.get("file:///C:/Users/VRUTIKA/Desktop/listbox.html");
489 driver.manage().window().maximize();
490 java.util.List<WebElement> optioncount=driver.findElements(By.xpath("//select/option"));
491 System.out.println("total no of items in listbox/combo-box are "+optioncount.size());
492 System.out.println("the name of the items are ");
493 for(int i=0;i<optioncount.size();i++)
494 {
495 System.out.println("item: "+(i+1)+" item name: "+optioncount.get(i).getText());
496 }
497 }
498}
499
500Output:
501
502Output for listbox.html:
503
504
505Output for listbox.java:
506
507
508Practical 9
509
510
511Aim: Write a program to count the number of Checkboxes Checked or Unchecked.
512
513Description:
514
515org.openqa.selenium
516public abstract class By extends Object - Mechanism used to locate elements within a document. In order to create your own locating mechanisms, it is possible to subclass this class and override the protected methods as required, though it is expected that all subclasses rely on the basic finding mechanisms provided through static methods of this class.
517public static By tagName(java.lang.String tagName)
518Parameters: tagName - The element's tag name.
519Returns: A By which locates elements by their tag name.
520
521public interface WebDriver extends SearchContext - The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:
522• Control of the browser itself
523• Selection of WebElements
524• Debugging aids
525java.util.List<WebElement> findElements(By by) - Find all elements within the current page using the given mechanism. This method is affected by the 'implicit wait' times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.
526Parameters: by - The locating mechanism to use
527Returns: A list of all WebElements, or an empty list if nothing matches
528
529void get(java.lang.String url) - Load a new web page in the current browser window.
530Parameters: url - The URL to load. It is best to use a fully qualified URL
531
532WebDriver.Options manage() - Gets the Option interface
533Returns: An option interface
534
535WebDriver.Window window() - Returns the interface for managing the current window.
536
537public interface WebElement extends SearchContext, TakesScreenshot - Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.
538
539org.openqa.selenium.firefox
540
541public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
542The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
543
544public static String setProperty(String key, String value ) - The setProperty() method of Java system class sets the property of the system which is indicated by a key.
545Parameters:
546key - It is the name of the system property.
547value - It is the value of the system property.
548
549Source Code:
550
551checkbox.html
552 <!DOCTYPE HTML>
553<html>
554 <body>
555 <br>
556 <h2>To checked and unchecked checkboxes in the web page</h2>
557 <form>
558 <input type="checkbox">SSC<br>
559 <input type="checkbox">HSC<br>
560 <input type="checkbox">BSC-CS<br>
561 <input type="checkbox">BSC-IT<br>
562 <form>
563 </body>
564</html>
565
566checkbox.jsp
567import org.openqa.selenium.By;
568import org.openqa.selenium.WebDriver;
569import org.openqa.selenium.WebElement;
570import org.openqa.selenium.firefox.FirefoxDriver;
571public class checkbox {
572 public static void main(String[] args) {
573 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
574 WebDriver driver = new FirefoxDriver();
575 driver.get("file:///C:/Users/VRUTIKA/Desktop/checkbox.html");
576 driver.manage().window().maximize();
577 java.util.List<WebElement> optioncount=driver.findElements(By.xpath("//input[@type='checkbox']"));
578 for(int i=0;i<optioncount.size();i=i+2)
579 {
580 optioncount.get(i).click();
581 }
582 int checkedcount=0,uncheckedcount=0;
583 for(int i=0;i<optioncount.size();i++)
584 {
585 System.out.println(i + " checkbox is selected: " + optioncount.get(i).isSelected());
586 if(optioncount.get(i).isSelected())
587 {
588 checkedcount++;
589 }
590 else
591 {
592 uncheckedcount++;
593 }
594 }
595 System.out.println("total number of checked boxes: "+checkedcount);
596 System.out.println("total number of unchecked boxes: "+uncheckedcount);
597 }
598}
599
600Output:
601
602Output for checkbox.html:
603
604
605Output for checkbox.java
606
607
608Practical 10
609
610
611Aim:TestNG installation for TestNG based practicals.
612
613Description:
614
615Steps to Install TestNG:
6161. Open Eclipse
6172. Go to Help menu >> Install New Software >> Click on Add >> Add Repository dialogue box and Provide name as “TestNG”
618
619
620
6213. For Location URL:
622a. Open Chrome
623b. Type “https://testng.org/doc/download.html” in search bar
624c. In “section Install from update” Click on link https://beust.com/eclipse. and copy the URL
625d. Paste the URL in textbox for Location and Click on “Add”
626
627
628
6294. After adding Repository select the “TestNG” package and Click on “Next”
630
6315. Click on Next >> Accept Lisence >> Next >> Finish
6326. A pop up will ask if you want to install an unsigned software. Click on “Install Anyway”
633
6347. Once TestNG is installed a pop will appear asking you to restart Eclipse IDE. Click on “Restart Now”
635
636
637Output:
638To check whether TestNG is installed or not: