🛑 Top 29 Selenium Commands That You Should Know As QA
Tags: testing, tutorial, beginners, webdev
Published: 2020-06-19 04:58:15

In this tutorial, we are going to learn Top 29 Selenium Commands that You Should Know As QA.

These are the exact commands that we have used in the Past and definitely can help you to create your automation scripts better.

🚀Source code:  https://scrolltest.com/automation/day12

[![Selenium Commands ](https://i.imgur.com/hXI7mDv.png)](https://youtu.be/cZW8UYCACs0 "Selenium Commands ") 


### 🚀 Selenium Commands

Start with Initialise the WebDriver 

1: Open a URL
```java
driver.get("https://google.com")
```
- This launches the Browser which is initialized

2: Get Current URL as Text
```java
driver.getCurrentUrl()
```
- Get the Current Page URL as String.

3: Get the Title 
```java
driver.getTitle()
```
- Get the Title of the Current Page.

4: Get Window Handle
```java
driver.getWindowHandle();
```
- Useful when we have Multiple Windows to Handle
- Returns String( window hash code)
- Useful to switch to other window.
```java
String mainWindow = driver.getWindowHandle();
			driver.switchTo().window(mainWindow);
```

5: Get Window Handles
```java
driver.getWindowHandles();
```	
- Useful when we have Multiple Windows to Handle
- Returns Array of windows( window hash code)
- Useful to switch to other window.
```java
Set<String> handles= driver.getWindowHandles();			driver.switchTo().window(“”windowName);
```java

6: DropDown Handling: Select Class
	- Use to work with Select Tag
```java
Select selectByValue = new Select(driver.findElement(By.id("SelectID")));
selectByValue.selectByVisibleText(“Pramod”);
selectByValue.selectByValue(“Dutta”);
```

7: Select an element by Index
```java
selectByValue.selectByIndex(2);
```

8: Input text in the input box.
```java
driver.findElement(By.id("tinymce")).sendKeys("Hello Pramod");
```

9: Submit the form
```java
        driver.findElement(By.id("tinymce")).submit();
```

10: Switch to Iframe
```java
driver.get("https://the-internet.herokuapp.com/iframe");
        driver.manage().window().maximize();
        driver.switchTo().frame("mce_0_ifr");
```

11: Close and quit the browser
```java
        driver.quit();
        driver.close();
```

12: Check whether the element is enabled or disabled.
		isEnabled()
```java
driver.findElement(By.id("tinymce")).isEnabled();
```

13: Get the text of the element
```java
driver.findElement(By.id("tinymce")).getText();
```

14: Find element size
```java
        int width = driver.findElement(By.id("tinymce")).getSize().width;
```

15: What is pageLoadTimeout?

Selenium defines different timeouts and wait for mechanisms. One of the timeouts is focused on the time a webpage needs to be loaded – the pageLoadTimeout limits the time that the script allows for a web page to be displayed. 
Syntax. : pageLoadTimeout(time,unit)
```java		driver.manage().timeouts().pageLoadTimeout(500, SECONDS);
```

16: Wait Until and visibilityOfElementLocated.
```java
	 WebDriverWait wait = new WebDriverWait(driver, 40);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tinymce")));
```java

17: alertIsPresent()
```java
WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
if(wait.until(ExpectedConditions.alertIsPresent())==null)
    System.out.println("alert was not present");
else
    System.out.println("alert was present");
```

18: Navigate to, back, forward within Browser.
		
```java			driver.navigate().to("https://scrolltest.com”);
			driver.navigate().back();
			driver.navigate().forward();
```

19: getScreenshotAs() To Take Screenshot in Selenium.
```java
 TakesScreenshot scrShot =((TakesScreenshot)driver);
        File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
        File DestFile=new File("./b.png");
        FileUtils.copyFile(SrcFile, DestFile);
```


20: moveToElement
		Action class
```java
Actions builder = new Actions(driver);
                Action mouseOverHome = builder
                        .moveToElement(link_Home)
                        .build();
```

21: dragAndDrop()
We can automate drag and drop of such elements using Selenium Webdriver.
```java

Actions act=new Actions(driver);					

	//Dragged and dropped.		
         act.dragAndDrop(From, To).build().perform();	
```


22: Alert switchTo() and accept(), dismiss()
```java
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();
driver.switchTo().alert().getText();			
driver.switchTo().alert().sendKeys("Text");
```

23: POI to read excel
Recommenced to Read - https://mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/


24: Database connection 
```java
DriverManager.getConnection(URL, "username", "password" )
```	

25: Asserts with Junit.
```java
Assert.assertNotEquals(message, “This text”);
		Assert.assertTrue(result<0);
		Assert.assertFalse(result<0);
		Assert.assertEquals(message, “This text”);
```
		

26: quit()
```java
// Quit Driver.
        driver.quit();
```

27: Clear Input box
```java
driver.findElement(By.id("User")).clear();
```

28: Click an element
```java
driver.findElement(By.id("User")).click();
```

29: getSize() of element
```java
 int width = driver.findElement(By.id("tinymce")).getSize().width;
```

✅Learn Jenkins for QA -
https://bit.ly/learnjenkins-p1

✅Programming Java -
https://bit.ly/learnjava2020-p1

✅Test Automation - 
https://bit.ly/learnautomation2020

✅API Testing - 
https://www.learnapitesting.com

✅Cypress Tutorial with LIVE Projects -
https://cypresstutorial.com

--
Be sure to subscribe for more videos like this!

[![ TheTestingAcademy](https://dev-to-uploads.s3.amazonaws.com/i/r7vcy6wbvyjbk1y1uu0o.png
)](https://www.youtube.com/TheTestingAcademy?sub_confirmation=1 "TheTestingAcademy")