Advertisement
NexGenration

Selenium LEX 7

Dec 4th, 2021
1,713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package com.test;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.concurrent.TimeUnit;
  5. import org.apache.commons.io.FileUtils;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.OutputType;
  11. import org.openqa.selenium.TakesScreenshot;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. public class Demo013_TakeScreenshot_ExceptionHandling {
  15.     WebDriver driver;
  16.     String url = "http://localhost:8080/PackAndGo_v2/index.html";
  17.  
  18.     @Before
  19.     public void setUp() {
  20.         //Set the key/value property according to the browser you are using.
  21.         System.setProperty("webdriver.gecko.driver",driverPath+"geckodriver.exe");
  22.                  
  23.         //Open browser instance
  24.         driver = new FirefoxDriver();
  25.                        
  26.         //Open the AUT
  27.         driver.get(url);
  28.        
  29.         //Declare an implicit wait which is bounded to WebDriver instance
  30.         driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
  31.     }
  32.     @Test
  33.     public void test() throws Exception {
  34.         //Click on Login
  35.         driver.findElement(By.xpath("//*[@id=\"myNavbar\"]/ul/li[4]/a")).click();      
  36.                    
  37.         Thread.sleep(2000);
  38.         //Enter the username
  39.         driver.findElement(By.id("usernameLogin")).sendKeys("pgGru");
  40.            
  41.         //Enter the password
  42.         driver.findElement(By.id("passwordLogin")).sendKeys("freezeray");
  43.            
  44.         //Click on Login button
  45.         driver.findElement(By.id("login")).click();
  46.        
  47.         //Post login take a screenshot of the dashboard page
  48.        
  49.         //Typecast the driver reference variable with TakesScreenshot for access the methods from TakesScreenshot interface
  50.         //getScreenshotAs method will take arguement for the output type of the file
  51.         File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  52.        
  53.         try{
  54.             FileUtils.copyFile(scrFile, new File("C:\\Users\\some_user\\Desktop\\Image.png"));
  55.         } catch(IOException e){
  56.             System.out.println("Exception Message: " + e.getMessage());
  57.         }      
  58.  
  59.         //Click on logout link
  60.         driver.findElement(By.linkText("LogOut")).click();
  61.     }
  62.    
  63.     @After
  64.     public void tearDown() throws Exception {
  65.         driver.close();
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement