Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Monday, January 25, 2021

Mouse Hover in Selenium using Java

What is Mouse Hover?

 Mouse hover is a function when we keep mouse cursor on a specific area like URL link or drop-down menu.

 Example:

  1. If I mouse hover on a URL link, it's green color can be changed to other
  2. If I mouse hover on a drop-down menu, it may open sub-menu.

Mouse Hover action in selenium JAVA

//1. Instantiate Action Class
Actions actions = new Actions(driverBrowser);

 

//2. Retrieve WebElement 'Music' to perform mouse hover
WebElement menuOption = driverBrowser.findElement(By.xpath(".//div[contains(text(),'Music')]"));

 

//3. Mouse hover menuOption 'Music'
actions.moveToElement(menuOption).perform();
System.out.println("Done Mouse hover on 'Music' from Menu");



Saturday, December 2, 2017

How to reuse a browser session for tests using selenium?

In Selenium 2 with WebDriver you can call
driver = new FirefoxDriver();
which spawns a browser, and that browser will stay open for the duration of your testing,
or you can choose to close it with driver.Quit().
Actually what I want to do?
I like to close my browser window between tests so that I know my tests aren't "dirty" with stored session data that could affect how the tests run, but I can see value in some targeted tests, where I want to try a couple different scenarios while keeping the same session going.
For code level: you can check is it null or not. If null then call the browser to give another.
/**
 * Driver for web application.
 * 
 * @return driver Browser
 * @throws IOException
 */
public WebDriver getDriverBrowser() throws IOException {

if (driverBrowser == null) {
    String sBrowser = PropertyLoader.loadProperty("browser");
    driverBrowser = getBrowser(sBrowser);
    driverBrowser
            .manage()
            .timeouts()
            .implicitlyWait(
                    Integer.valueOf(PropertyLoader
                            .loadProperty("implicit_timeout_sec")),
                    TimeUnit.SECONDS);
    driverBrowser.manage().window().maximize();
}
return driverBrowser;
}
Or you can use xml configuration to use some test cases as class level or test specific.Using TestNG, you can specify which tests you want to run (TestNG will generate an XML file of all of the tests that fail, so when you run it, it will only execute the failed tests).
Issue - 1: I mean i don't want to log in again to continue testing , I don't mind if it opens another window, in fact as you said it's more reliable but not helpful if i have to log in again.
If you don't want to login, then in every testcase, you need a base starting point. After ending any test case, it will go to base position and next testcase will start from the base
Issue - 2: How can i implement a base starting point?
It is up to you. First you have to analyze the test cases, then you have to select which point can be the starting point for all or set of some test cases. Then you can do it.
For more:
  1. https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/3927
  2. https://sqa.stackexchange.com/questions/1988/selenium-reuse-existing-browser-session-instead-of-opening-new-windows

Friday, December 1, 2017

How to get a current class name and methods that executed in the class file using TestNG listeners?

Answer:

Use ITestResultITestContext and IMethodInstance which can give you class name and method name which are using.
for (ITestResult testResult : testResultSet) {
    String methodName = testResult.getName() + "(";
    for (Object arg : testResult.getParameters()) {
        methodName += arg == null ? "" : arg.toString() + ", ";
    }

    uniqueMethodMap.add(testResult.getName());
    methodList.add(testResult.getParameters().length > 0 ?    methodName.substring(0, methodName.length() - 2) + ")" : methodName + ")");
}