· 6 years ago · Aug 29, 2019, 01:08 PM
1Practical 1
2
3Aim: Install Selenium IDE: write a test suite containing minimum 4 test cases for different formats.
4
5Description:
6
7Installation of Selenium IDE
8Launch Mozilla Firefox Browser
9Type URL https://www.seleniumhq.org/download/
10OR
11https://addons.mozilla.org/en-US/firefox/addon/selenium-ide
12Selenium IDE Add-ons page will get open then Click on Add to Firefox button
13Firefox will show one popup saying do you want to allow Mozilla Firefox to install Selenium IDE Add-ons or not. Click on Install button.
14Firefox 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
15On clicking on the Restart Now button, Firefox will restart automatically. In case you missed the pop-up, simply close the Firefox and launch again.
16Once 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
17
18Testing Websites
19Launch Selenium IDE, after launching a pop up appears like shown below, select “Record a new test in a new project”
20
21Enter name of the project, click on “OK”
22
23Enter Base URL for your project, and click on “START RECORDING”
24
25Once 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
26
27
28Once you click on stop recording icon a pop as shown below will ask for test name. Provide test name and click on “OK”
29
30Once saved you can run your test again. To run the test again click on “Run Current Test” icon
31
32your test will run in the browser
33Repeat the above steps with three more websites
34https://www.facebook.com
35https://www.twitter.com
36https://www.instagram.com
37
38
39###########-----Practical 3--------########
40
41
42Aim: Install Selenium server and demonstrate it using a script in Java/PHP.
43
44Description:
45
46Download Selenium server: http:/scleniumhq.org/download/
47Download Selenium Client driver for Java (from Selenium Client Drivers section)
48Open Chrome and type “java jdk” in search bar
49Click on Java SE - Downloads | Oracle Technology Network >> Download >> Accept license for JDK 12.0.2 >> jdk-12.0.2_windows-x64_bin.exe
50Open File Explorer and open the following folder C:\jdk-12.0.2\bin and copy the address from address bar
51Open 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
52Open cmd and execute following commands
53java -version
54javac
55Open Chrome and type “Eclipse” in search bar
56Click on Eclipse Downloads | The Eclipse Foundation >> Download 64-bit >> Download
57Once downloaded, open Eclipes Installer. Click on icon >> Update >> Accept >> OK
58Select Eclipse IDE for Java Developers >> Install >> Accept now >> Accept >> Select All >> Accept >> Launch. This should open a welcome screen.
59Close welcome screen tab and Click on File >> New >> Java Project >> Name your project >> Finish.
60Once 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.
61Once you create a java class type the source code and run application
62
63Source Code:
64
65import java.io.BufferedReader;
66import java.io.IOException;
67import java.io.InputStreamReader;
68
69public class operation {
70 public static void main(String[] args) throws IOException {
71 int a, b, c;
72 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
73 System.out.println("Enter a number: ");
74 a = Integer.parseInt(br.readLine());
75 System.out.println("Enter a number: ");
76 b = Integer.parseInt(br.readLine());
77 c = a + b;
78 System.out.println("Sum of two numbers: " + c);
79 }
80}
81
82########-----------Practical 4----------+-######
83
84Aim: Write and test a program to login a specific web page.
85
86Description:
87
88Installing Window Builder in Eclipse
89Open Eclipse IDE
90Go to Help menu
91Select 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”.
92Then it will ask you to restart Eclipse Click on “Yes”
93
94Steps to Create a Login page
95Open Eclipse IDE
96Go to File menu >> New >> Java Project >> Name your project >> Next >> Finish
97Once 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.
98Go 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
99Now Select a JButton and drop it on Frame >> Change its text property to Login >> Right Click on JButton >> Add event handler >> action >> actionPerformed
100Type the source code and Run the application.
101
102Source Code:
103
104public void actionPerformed(ActionEvent arg0)
105{
106 String uname= textField.getText();
107 String pwd=passwordField.getText();
108 if(uname.equals("Username") && pwd.equals("Password"))
109 JOptionPane.showMessageDialog(frame, "You are Logged in");
110 else
111 JOptionPane.showMessageDialog(frame, "Invalid Username or Password");
112 }
113
114#####################-------------#####################
115
116Practical 5
117
118
119Aim: Write and test program to update 5 students records into table into Excel file.
120
121Description:
122
123JXL – Java Excel API
124JXL 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.
125JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls.
126JXL API is widely used with Selenium.
127
128jxl.write – This package includes all the interfaces and classes that allows user to wirte or modify data in excel sheets.
129Interface - WriteableSheet
130Class - Number
131
132
133public 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.
134Sheet createSheet(java.lang.String sheetname)
135Create a new sheet for this Workbook and return the high level representation. Use this to create new sheets.
136Excel 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.
137
138public 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.
139addCell(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.
140
141Source Code:
142
143import jxl.Workbook;
144import jxl.write.*;
145import jxl.write.Number;
146//import org.openqa.selenium.remote.html5.AddLocationContext;
147import java.io.File;
148import java.io.IOException;
149
150public class webclass {
151 private static final String EXCEL_FILE_LOCATION = "D:\\StudentsRecord.xls";
152 public static void main(String[] args) {
153
154 String names[]={"Ram","Jai","Shyam","Raj","Rahul"};
155 int marks[]={60,70,50,65,80};
156 //1. Create an Excel file
157 WritableWorkbook myFirstWbook = null;
158 try {
159
160 myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));
161
162 // create an Excel sheet
163 WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);
164
165 // add something into the Excel sheet
166 Label label = new Label(0, 0, "Roll. No.");
167 excelSheet.addCell(label);
168
169 for(int i=0;i<5;i++)
170 {
171 Number number = new Number(0, i+1, i+1);
172 excelSheet.addCell(number);
173 }
174
175 label = new Label(1, 0, "Student Name");
176 excelSheet.addCell(label);
177
178 for(int i=0;i<5;i++)
179 {
180 Label name = new Label(1, i+1, names[i]);
181 excelSheet.addCell(name);
182 }
183
184 label = new Label(2, 0, "Marks");
185 excelSheet.addCell(label);
186
187 for(int i=0;i<5;i++)
188 {
189 Number number = new Number(2, i+1,marks[i]);
190 excelSheet.addCell(number);
191 }
192 myFirstWbook.write();
193 } catch (IOException e) {
194 e.printStackTrace();
195 } catch (WriteException e) {
196 e.printStackTrace();
197 } finally {
198
199 if (myFirstWbook != null) {
200 try {
201 myFirstWbook.close();
202 } catch (IOException e) {
203 e.printStackTrace();
204 } catch (WriteException e) {
205 e.printStackTrace();
206 }
207 }
208 }
209 }
210}
211
212#####---------++###############______-------
213
214Practical 6
215
216
217Aim: Write and test a program to read data from an Excel file.
218
219Description:
220
221JXL – Java Excel API
222JXL 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.
223JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls.
224JXL API is widely used with Selenium.
225
226public 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.
227getWorkbook(java.io.File file) - A factory method which takes in an excel file and reads in the contents.
228getSheet(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).
229
230public interface Cell - Represents an individual Cell within a Sheet. May be queried for its type and its content
231public 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
232
233public interface Sheet - Represents a sheet within a workbook. Provides a handle to the individual cells, or lines of cells (grouped by Row or Column)
234public 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
235Parameters:
236column - the column number
237row - the row number
238Returns: the cell at the specified co-ordinates
239
240Source Code:
241
242import jxl.Cell;
243import jxl.Sheet;
244import jxl.Workbook;
245import jxl.read.biff.BiffException;
246import java.io.File;
247import java.io.IOException;
248
249public class readfile {
250
251 private static final String EXCEL_FILE_LOCATION = "D:\\StudentsRecord.xls";
252
253 public static void main(String[] args) {
254 Workbook workbook = null;
255 try {
256 workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION));
257 Sheet sheet = workbook.getSheet(0);
258 Cell cell1 = sheet.getCell(0, 0);
259 Cell cell2 = sheet.getCell(1, 0);
260 Cell cell3 = sheet.getCell(2, 0);
261 for(int i=0;i<5;i++)
262 {
263 Cell cell = sheet.getCell(0, i+1);
264 System.out.println(cell1.getContents()+": "+cell.getContents());
265 Cell celln = sheet.getCell(1, i+1);
266 System.out.println(cell2.getContents()+": "+celln.getContents());
267 Cell cellm = sheet.getCell(2, i+1);
268 System.out.println(cell3.getContents()+": "+cellm.getContents()+"\n");
269 }
270 } catch (IOException e) {
271 e.printStackTrace();
272 } catch (BiffException e) {
273 e.printStackTrace();
274 } finally {
275 if (workbook != null) {
276 workbook.close();
277 }
278 }
279 }
280}
281
282###########----#######--------------#######-------
283
284Practical 7
285
286
287Aim: Write a program to count the number of objects present/available on the page.
288
289Description:
290
291org.openqa.selenium
292public 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.
293public static By tagName(java.lang.String tagName)
294Parameters: tagName - The element's tag name.
295Returns: A By which locates elements by their tag name.
296
297public 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:
298Control of the browser itself
299Selection of WebElements
300Debugging aids
301java.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.
302Parameters: by - The locating mechanism to use
303Returns: A list of all WebElements, or an empty list if nothing matches
304
305void get(java.lang.String url) - Load a new web page in the current browser window.
306Parameters: url - The URL to load. It is best to use a fully qualified URL
307
308WebDriver.Options manage() - Gets the Option interface
309Returns: An option interface
310
311WebDriver.Window window() - Returns the interface for managing the current window.
312
313public 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.
314
315org.openqa.selenium.firefox
316public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
317The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
318
319public 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.
320Parameters:
321key - It is the name of the system property.
322value - It is the value of the system property.
323
324Source Code:
325
326file.html
327<!DOCTYPE HTML>
328<html>
329 <body>
330 <h2>To check the links present in web page</h2>
331 <a href="https://www.google.com">Google</a><br>
332 <a href="https://www.youtube.com">Youtube</a><br>
333 <a href="https://www.facebook.com">facebook</a><br>
334 <a href="https://www.gmail.com">Gmail</a><br>
335 </body>
336</html>
337
338links.java
339import org.openqa.selenium.By;
340import org.openqa.selenium.WebDriver;
341import org.openqa.selenium.WebElement;
342import org.openqa.selenium.firefox.FirefoxDriver;
343public class links {
344 public static void main(String[] args)
345 {
346 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
347 WebDriver driver = new FirefoxDriver();
348 driver.get("file:///C:/Users/VRUTIKA/Desktop/links.html");
349 driver.manage().window().maximize();
350 java.util.List<WebElement> links=driver.findElements(By.tagName("a"));
351 System.out.println("total no of links are: "+links.size());
352 System.out.println("the name of the links are: ");
353 for(int i=0;i<links.size();i++)
354 {
355 System.out.println("link: "+(i+1)+" link name: "+links.get(i).getText());
356 }
357 }
358}
359
360###########------------------------------#############
361
362Practical 8
363
364
365Aim: Write a program to get the number of items from a list box/combobox from a webpage.
366
367Description:
368
369org.openqa.selenium
370public 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.
371public static By tagName(java.lang.String tagName)
372Parameters: tagName - The element's tag name.
373Returns: A By which locates elements by their tag name.
374
375public 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:
376Control of the browser itself
377Selection of WebElements
378Debugging aids
379java.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.
380Parameters: by - The locating mechanism to use
381Returns: A list of all WebElements, or an empty list if nothing matches
382
383void get(java.lang.String url) - Load a new web page in the current browser window.
384Parameters: url - The URL to load. It is best to use a fully qualified URL
385
386WebDriver.Options manage() - Gets the Option interface
387Returns: An option interface
388
389WebDriver.Window window() - Returns the interface for managing the current window.
390
391public 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.
392
393org.openqa.selenium.firefox
394public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
395The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
396
397public 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.
398Parameters:
399key - It is the name of the system property.
400value - It is the value of the system property.
401
402Source Code:
403
404listbox.html
405<!DOCTYPE HTML>
406<html>
407 <body>
408 <h2>To check the items of combo-box present in the web page</h2>
409 <select name="menu">
410 <option value="0" selected>zero</+option>
411 <option value="1">one</option>
412 <option value="2">two</option>
413 <option value="3">three</option>
414 <option value="others">four</option>
415 </select>
416 <br>
417 </body>
418</html>
419
420listbox.java
421import org.openqa.selenium.By;
422import org.openqa.selenium.WebDriver;
423import org.openqa.selenium.WebElement;
424import org.openqa.selenium.firefox.FirefoxDriver;
425public class listbox {
426
427 public static void main(String[] args) {
428 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
429 WebDriver driver = new FirefoxDriver();
430 driver.get("file:///C:/Users/VRUTIKA/Desktop/listbox.html");
431 driver.manage().window().maximize();
432 java.util.List<WebElement> optioncount=driver.findElements(By.xpath("//select/option"));
433 System.out.println("total no of items in listbox/combo-box are "+optioncount.size());
434 System.out.println("the name of the items are ");
435 for(int i=0;i<optioncount.size();i++)
436 {
437 System.out.println("item: "+(i+1)+" item name: "+optioncount.get(i).getText());
438 }
439 }
440}
441
442################-------------------------------########
443
444Practical 9
445
446
447Aim: Write a program to count the number of Checkboxes Checked or Unchecked.
448
449Description:
450
451org.openqa.selenium
452public 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.
453public static By tagName(java.lang.String tagName)
454Parameters: tagName - The element's tag name.
455Returns: A By which locates elements by their tag name.
456
457public 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:
458Control of the browser itself
459Selection of WebElements
460Debugging aids
461java.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.
462Parameters: by - The locating mechanism to use
463Returns: A list of all WebElements, or an empty list if nothing matches
464
465void get(java.lang.String url) - Load a new web page in the current browser window.
466Parameters: url - The URL to load. It is best to use a fully qualified URL
467
468WebDriver.Options manage() - Gets the Option interface
469Returns: An option interface
470
471WebDriver.Window window() - Returns the interface for managing the current window.
472
473public 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.
474
475org.openqa.selenium.firefox
476
477public class FirefoxDriver extends RemoteWebDriver implements WebStorage - An implementation of the {#link WebDriver} interface that drives Firefox.
478The best way to construct a FirefoxDriver with various options is to make use of the FirefoxOptions
479
480public 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.
481Parameters:
482key - It is the name of the system property.
483value - It is the value of the system property.
484
485Source Code:
486
487checkbox.html
488 <!DOCTYPE HTML>
489<html>
490 <body>
491 <br>
492 <h2>To checked and unchecked checkboxes in the web page</h2>
493 <form>
494 <input type="checkbox">SSC<br>
495 <input type="checkbox">HSC<br>
496 <input type="checkbox">BSC-CS<br>
497 <input type="checkbox">BSC-IT<br>
498 <form>
499 </body>
500</html>
501
502checkbox.jsp
503import org.openqa.selenium.By;
504import org.openqa.selenium.WebDriver;
505import org.openqa.selenium.WebElement;
506import org.openqa.selenium.firefox.FirefoxDriver;
507public class checkbox {
508 public static void main(String[] args) {
509 System.setProperty("webdriver.gecko.driver","C:\\Users\\VRUTIKA\\Desktop\\geckodriver.exe");
510 WebDriver driver = new FirefoxDriver();
511 driver.get("file:///C:/Users/VRUTIKA/Desktop/checkbox.html");
512 driver.manage().window().maximize();
513 java.util.List<WebElement> optioncount=driver.findElements(By.xpath("//input[@type='checkbox']"));
514 for(int i=0;i<optioncount.size();i=i+2)
515 {
516 optioncount.get(i).click();
517 }
518 int checkedcount=0,uncheckedcount=0;
519 for(int i=0;i<optioncount.size();i++)
520 {
521 System.out.println(i + " checkbox is selected: " + optioncount.get(i).isSelected());
522 if(optioncount.get(i).isSelected())
523 {
524 checkedcount++;
525 }
526 else
527 {
528 uncheckedcount++;
529 }
530 }
531 System.out.println("total number of checked boxes: "+checkedcount);
532 System.out.println("total number of unchecked boxes: "+uncheckedcount);
533 }
534}