2

In what class should I write: Actions actions = new Actions(driver); ?

I don't want actions to be hard-coded. This is how my PageObjects looks like, here I want to use Actions Class

package Selenium;

import java.rmi.activation.ActivationSystem;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;

public class internetPO extends basicPageObject {



GenericWebElement wd;


public internetPO(GenericWebElement remotewebdriver){
    this.wd=remotewebdriver;  
}
   public void hover(){
   Actions actions = new Actions(driver);
   WebElement ss= wd.element(byTypes.XPATH, finalElements.profileHover);
    actions.moveToElement(ss).perform();
     WebElement ww = wd.element(byTypes.XPATH, finalElements.profilename);

     String valid = ww.getText();
     System.out.println(valid);
}

And this is my TestClass:

package Selenium;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;

public class baseTest  {

GenericWebElement wd;
public WebDriver webdriver;

public internetPO inter;

@Rule
public TestName name = new TestName();
public TestWatcher log = new TestWatcher() {

};

public void Testname(Description testname){
    System.out.println("Starting test" + testname.getMethodName());

}

@Before
public void setup() throws Exception{
    String Testname = name.getMethodName();
    System.out.println("Strting test " + Testname);

    wd=new ChromeWebDriver();


    wd.init("http://localhost:4444/wd/hub");    

     inter = new internetPO(wd);
   }

Need to mention I'm using RemoteWebDriver in GenericWebElement class. I tried a lot of options but no luck.

1

The way that I did in my project is that I created a class as CustomActions which needs a Webdriver object for construction:

package utils.selenium;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

class CustomActions{

    private static final String JS_SCROLL_TO_TOP_PAGE = "scrollTo(0, 0)";
    private static final String JS_MOUSE_SCROLL_DRAFT = "scroll(%s, %s)";

    private Actions actions;
    private WebDriver webDriver;

    CustomActions(WebDriver driver) {
        this.webDriver = driver;
        this.actions = new Actions(this.webDriver);
    }

    void jsExecuteScript(String text){
        ((JavascriptExecutor) this.webDriver).executeScript(text);
    }

    void goToTopPage() {
         this.jsExecuteScript(JS_SCROLL_TO_TOP_PAGE);
    }

    void scroll(int horizontal, int vertical) {
        this.jsExecuteScript(String.format(JS_MOUSE_SCROLL_DRAFT, String.valueOf(horizontal), String.valueOf(vertical)));
    }

    void dragAndDropBy(WebElement element, int horizontal, int vertical) {
        this.actions.dragAndDropBy(element, horizontal, vertical).build().perform();
    }

}

So you can wrap any actions that you need here. Then in your driver class:

package utils.selenium;

public final class Driver {
    private static final long DEFAULT_TIMEOUT = 100;
    private static final long DEFAULT_SHORT_TIMEOUT = 1;

    private static Driver wrappedDriver;
    private static WebDriver webDriver;
    private Waiter waiter;
    private CustomActions customActions;

    private Driver(WebDriver driver) {
        this.waiter = new Waiter(driver);
        this.customActions = new CustomActions(driver);

        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    }

    ...// many other required things
    // Then your required actions:
    public void scrollToTopOfThePage() {
        customActions.goToTopPage();
    }

    public void scrollMouse(int horizontal, int vertical) {
        customActions.scroll(horizontal, vertical);
    }

    public void executeCustomJS(String js) {
        customActions.jsExecuteScript(js);
    }

    public void dragAndDropBy(WebElement element, int horizontal, int vertical) {
        customActions.dragAndDropBy(element, horizontal, vertical);
    }
}

It was just an idea that I used in my codes and I hope it gives you a good idea. Actually your question is really nice and I hope a more expert user comes here with a perfect answer. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.