0% found this document useful (0 votes)
74 views3 pages

What Is Action Class in Selenium?: 1) How To Mouse Hover On A Web Element Using Webdriver?

The Action class in Selenium handles keyboard and mouse events like double click, drag and drop, move to element etc. To send modifier keys like ALT, SHIFT, CONTROL in Selenium, we use keyDown() and keyUp() methods along with the modifier key. TestNG allows setting priority to test methods using the @Test annotation's priority attribute to control execution order. Basic get commands in Selenium are getCurrentUrl(), getPageSource(), getTitle() and getText(). There are two types of asserts in TestNG - hard assert which fails the test on failure, and soft assert which allows continuing execution after failure.

Uploaded by

tija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views3 pages

What Is Action Class in Selenium?: 1) How To Mouse Hover On A Web Element Using Webdriver?

The Action class in Selenium handles keyboard and mouse events like double click, drag and drop, move to element etc. To send modifier keys like ALT, SHIFT, CONTROL in Selenium, we use keyDown() and keyUp() methods along with the modifier key. TestNG allows setting priority to test methods using the @Test annotation's priority attribute to control execution order. Basic get commands in Selenium are getCurrentUrl(), getPageSource(), getTitle() and getText(). There are two types of asserts in TestNG - hard assert which fails the test on failure, and soft assert which allows continuing execution after failure.

Uploaded by

tija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) How to mouse hover on a web element using

WebDriver?
By using Actions class

WebElement ele = driver.findElement(By.xpath("xpath"));


//Create object 'action' of an Actions class
Actions action = new Actions(driver);
//Mouseover on an element
action.moveToElement(ele).perform();

What is Action Class in Selenium?


Handles keyboard and mouse events
Mouse Actions in Selenium:

1. doubleClick(): Performs double click on the element


2. clickAndHold(): Performs long click on the mouse without releasing it
3. dragAndDrop(): Drags the element from one point and drops to another
4. moveToElement(): Shifts the mouse pointer to the center of the element
1 5. contextClick(): Performs right-click on the mouse
2
3 Keyboard Actions in Selenium:
4
5
1. sendKeys(): Sends a series of keys to the element
2. keyUp(): Performs key release
3. keyDown(): Performs keypress without release
https://www.browserstack.com/guide/action-class-in-selenium
https://www.guru99.com/keyboard-mouse-events-files-webdriver.html

2) How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and
click other buttons to achieve the special functionality. So it is not enough just to
specify keys.ALT or keys.SHIFT or keys.CONTROL functions.

For the purpose of holding onto these keys while subsequent keys are pressed, we
need to define two more
methods: keyDown(modifier_key) and keyUp(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a modifier key press and does not release the modifier key.
Subsequent interactions may assume it’s kept pressed.

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)


Purpose: Performs a key release.
Hence with a combination of these two methods, we can capture the special
function of a particular key.

public static void main(String[] args)


{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();

driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);

Actions builder = new Actions(driver);


Action seriesOfActions = builder
 .moveToElement(txtUerName)
 .click()
 .keyDown(txtUserName, Keys.SHIFT)
 .sendKeys(txtUserName, “hello”)
 .keyUp(txtUserName, Keys.SHIFT)
 .doubleClick(txtUserName);
 .contextClick();
 .build();
seriesOfActions.perform();
}
https://www.edureka.co/blog/interview-questions/selenium-interview-questions-
answers/

3) How to set priorities in TestNG?


https://www.guru99.com/test-case-priority-testng.html
Answer: There are always more than one test or method in the class. If we do not
prioritize these tests or methods, then the methods are selected alphabetically and
executed while execution.
If we want to run the tests in the sequence we want, then we need to set the priority
along with the @Test annotation.

This can be done as follows:


@Test (priority=1), @Test (priority=2)

Consider the following Example:


@Test (priority=2)

public void getText()


{
driver.findElement(By.id(“id”)).getText();
}
@Test(priority=1)
public void clickelement()
{
driver.findElement(By.id(“id”)).click();
}
In the above example, clickelement() will get executed first as the priority is set to 1.

And, getText() will get executed after clickelement() as its priority is set to 2.

asic Get commands used in the


4)B

browser
Get commands or methods help QAs or developers to fetch specific parameters on
the target webpage to be tested. Let’s quickly go through some basic Get
commands in Selenium.

getCurrentUrl() – This command returns the URL of the currently active web page
in the browser.
getPageSource() – This command helps in getting the entire HTML source code of
the open web page.
getTitle() – This command can be used for displaying the title of the current web
page.
getText()-returns the innerText of an element.

5)  What are the types of Asserts in TestNG?


Answer: To validate the results (pass/fail), we have to use the assertion.
There are two types of assert in TestNG:
(i) Hard Assert:
Hard Assert is the normal assert which is used to do validations in the TestNG class.

We have to use Assert class for hard assert as follows:


Assert.assertEquals(actual value, expected value);

If the hard assert fails, then none of the code gets executed after the assert statement.

(ii) Soft Assert:


If we want to continue the test execution even after the assert statement fails, then we have
to use soft assert.

To create a soft assert, we have to create an object of a “softAssert” class as follows:


softAssert sassert = new softAssert();
sassert.assertAll();

So now if the test case fails, the execution is not terminated when we use soft assert.

You might also like