loading...
Cover image for πŸ›‘ Top 29 Selenium Commands That You Should Know As QA

πŸ›‘ Top 29 Selenium Commands That You Should Know As QA

promode profile image Pramod Dutta ・3 min read

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

Posted on Jul 5 by:

promode profile

Pramod Dutta

@promode

Lead Automation Expert || Founder of TheTestingAcademy.com || Youtube at TheTestingacademy || Udemy Teacher. || scrolltest.com

Discussion

markdown guide
 

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