NexGenration

Selenium LEX 6

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