Selenium 4 Beginner Tutorial 1 | Introduction, Setup & Browser Actions | Step by Step

Automation Step by Step October 19, 2021
Video Thumbnail
Automation Step by Step Logo

Automation Step by Step

@raghavpal

About

Simple Basic Beginners Step-by-Step tutorials ❤️ I create videos on Automation, Testing, DevOps, CI and related tools, technologies and platform Raghav is a teacher and founder of AutomationStepByStep.com He was an Automation Architect and has led multiple teams of Automation and DevOps Engineers. For over a decade, Raghav witnessed, worked, and delivered multiple Automation Testing Projects and worked with some awesome people in this industry ► https://automationstepbystep.com/ Some topics covered: - Jenkins - JMeter - Selenium - Appium - Cypress - Playwright - Katalon Studio - API Testing - Postman - Docker - Kubernetes - Other DevOps topics Words from Raghav We often need someone to hold our hand and help us take the first few steps before we learn to walk and run. I am on a mission to spread education and make it available to anyone willing to learn Never Stop Learning Raghav All education here is FREE FOR ALL, FOREVER Share with as many people as you can in your lifetime

Video Description

All FREE courses - https://automationstepbystep.com/ Selenium Quiz - https://automationstepbystep.com/quiz-time/ GitHub - https://github.com/Raghav-Pal/SeleniumProjectNov2021 00:00 Introduction 01:35 Selenium 4 03:48 Getting Started - Project Setup 09:31 1st Test 19:30 WebDriverManager 24:33 Browser Actions 25:25 How to open a web page 26:23 Get current URL 27:00 Get Title 27:53 Forward | Back | Refresh 29:44 Switching windows 32:12 Open new Window 33:26 Open new Tab 36:58 Close Browser 38:00 Frames 44:20 Get & Set Window size 48:49 Get & Set Window position 51:44 Maximize | Minimize | Full Screen 53:06 Screenshot 59:04 JavaScript Executor 01:03:58 Conclusion Selenium 4 -------------- New and improved version of Selenium Se webdriver can directly communicate with browser using W3C protocol (without use of json wire protocol as earlier) New functions added Multiple windows & tabs management Relative locators New Documentation Getting Started ------------------- Install Java Setup Eclipse Create a new maven project Add maven dependencies for Selenium 4 Download browser driver & add in a folder in project (Can also keep at any location on your system and add the location in path env var) Optional Add TestNG plugin Add TestNG dependency 1st Test --------- Step 1 - Create a class & main method public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe"); WebDriver driver = new ChromeDriver(); //timeout driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2)); driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10)); //close driver.close(); } Step 2 - Run code Using WebDriver Manager Step 1 - Add maven dependency for webdriver manager - https://bonigarcia.dev/webdrivermanager/ Step 2 - Add code WebDriverManager.chromedriver().setup(); Step 3 - To use specific ver of browser WebDriverManager.chromedriver().driverVersion("92.0").setup(); Browser Actions ---------------------- 1. Open a web page driver.get("https://google.com"); driver.navigate().to("https://selenium.dev"); 2. Get current url driver.getCurrentUrl(); 3. Get title driver.getTitle(); 4. Forward | Back | Refresh driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh(); 5. Switching windows String originalWindow = driver.getWindowHandle(); driver.switchTo().window(originalWindow); 6. Open new window and switch to the window driver.switchTo().newWindow(WindowType.WINDOW); 7. Open new tab and switch to the tab driver.switchTo().newWindow(WindowType.TAB); 8. Closing browser driver.close(); driver.quit(); 9. Frames Locate and Switch WebElement iframe = driver.findElement(By.cssSelector(".rightContainer>frame")); driver.switchTo().frame(iframe); Using id or name driver.switchTo().frame("classFrame"); Using index driver.switchTo().frame(1); Return to top level window driver.switchTo().defaultContent(); 10. Window management - Size Get width & height int width = driver.manage().window().getSize().getWidth(); int height = driver.manage().window().getSize().getHeight(); Store dimensions & query later Dimension size = driver.manage().window().getSize(); int width1 = size.getWidth(); int height1 = size.getHeight(); Set window size driver.manage().window().setSize(new Dimension(800, 600)); 10. Window management - Position Access x and y dimensions individually int x = driver.manage().window().getPosition().getX(); int y = driver.manage().window().getPosition().getY(); Store dimensions & query later Point position = driver.manage().window().getPosition(); int x1 = position.getX(); int y1 = position.getY(); Move the window to the top left of the primary monitor driver.manage().window().setPosition(new Point(0, 0)); 10. Window management // maximize window driver.manage().window().maximize(); // minimize window driver.manage().window().minimize(); // fullscreen driver.manage().window().fullscreen(); 11. Screenshots Take screenshot File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("./image.png")); Take element screenshot WebElement element = driver.findElement(By.cssSelector(".lnXdpd")); File scrFile1 = element.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile1, new File("./image1.png")); 12. JavaScript Create JavascriptExecutor interface object by Type casting JavascriptExecutor js = (JavascriptExecutor)driver; Get return value from script WebElement button =driver.findElement(By.name("btnI")); String text = (String) js.executeScript("return arguments[0].innerText", button); JavaScript to click element js.executeScript("arguments[0].click();", button); Execute JS directly js.executeScript("console.log('hello world')"); #Selenium4Tutorials If my work has helped you, consider helping any animal near you, in any way you can. Never Stop Learning Raghav

You May Also Like