DEV Community

Cover image for 🛑 Top 29 Selenium Commands That You Should Know As QA
Pramod Dutta
Pramod Dutta

Posted on • Edited on

🛑 Top 29 Selenium Commands That You Should Know As QA

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

🚀 Selenium Commands

Start with Initialise the WebDriver

1: Open a URL

driver.get("https://google.com")
  • This launches the Browser which is initialized

2: Get Current URL as Text

driver.getCurrentUrl()
  • Get the Current Page URL as String.

3: Get the Title

driver.getTitle()
  • Get the Title of the Current Page.

4: Get Window Handle

driver.getWindowHandle();
  • Useful when we have Multiple Windows to Handle
  • Returns String( window hash code)
  • Useful to switch to other window.
String mainWindow = driver.getWindowHandle();
            driver.switchTo().window(mainWindow);

5: Get Window Handles

driver.getWindowHandles();
  • Useful when we have Multiple Windows to Handle
  • Returns Array of windows( window hash code)
  • Useful to switch to other window.
Set<String> handles= driver.getWindowHandles();         driver.switchTo().window(“”windowName);


java

6: DropDown Handling: Select Class
- Use to work with Select Tag

Select selectByValue = new Select(driver.findElement(By.id("SelectID")));
selectByValue.selectByVisibleText(“Pramod”);
selectByValue.selectByValue(“Dutta”);

7: Select an element by Index

selectByValue.selectByIndex(2);

8: Input text in the input box.

driver.findElement(By.id("tinymce")).sendKeys("Hello Pramod");

9: Submit the form

        driver.findElement(By.id("tinymce")).submit();

10: Switch to Iframe

driver.get("https://the-internet.herokuapp.com/iframe");
        driver.manage().window().maximize();
        driver.switchTo().frame("mce_0_ifr");

11: Close and quit the browser

        driver.quit();
        driver.close();

12: Check whether the element is enabled or disabled.
isEnabled()

driver.findElement(By.id("tinymce")).isEnabled();

13: Get the text of the element

driver.findElement(By.id("tinymce")).getText();

14: Find element size

        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()

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

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.


Actions act=new Actions(driver);                    

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

22: Alert switchTo() and accept(), dismiss()

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

DriverManager.getConnection(URL, "username", "password" )

25: Asserts with Junit.

Assert.assertNotEquals(message, “This text”);
        Assert.assertTrue(result<0);
        Assert.assertFalse(result<0);
        Assert.assertEquals(message, “This text”);

26: quit()

// Quit Driver.
        driver.quit();

27: Clear Input box

driver.findElement(By.id("User")).clear();

28: Click an element

driver.findElement(By.id("User")).click();

29: getSize() of element

 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

Top comments (2)

Collapse
 
muhimen123 profile image
Muhimen

At first, I thought it was Python. Never mind.

Collapse
 
promode profile image
Pramod Dutta

haha alright