0

I have my tests automated with Webdriver+Java+TestNG+Maven

I am looking for a solution wherein the progress (Step Into next step) of the test can be controlled using keyboard interrupts on every navigation.

Ex: Lets say we are automation navigation of an application. The progress of the test should be driven by a key press for every page redirection.

I have partially found the solution. I used the code from github - https://gist.github.com/krmahadevan/1728633

Test Class -

import com.shn.library.WebDriverListener;

public class DummyTest {

        @Test
        public void testMethod(){
            WebDriver driver = new FirefoxDriver();
            EventFiringWebDriver efwd = new EventFiringWebDriver(driver);
            WebDriverListener eventListener = new WebDriverListener(efwd);
            efwd.register(eventListener);
            efwd.get("http://www.yahoo.com");
            efwd.get("https://www.mail.google.com");

        }
    }

Implementing WebDriverEventListener -

package com.shn.library;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.concurrent.CountDownLatch;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class WebDriverListener implements WebDriverEventListener {
    private WebDriver webDriver;

    public WebDriverListener(WebDriver webDriver){
        this.webDriver = webDriver;
    }

    public void beforeNavigateTo(String url, WebDriver driver) {

    }

    public void afterNavigateTo(String url, WebDriver driver) {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            // Anonymous class invoked from EDT
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE)
                    latch.countDown();
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        try {
            latch.await();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }  // current thread waits here until countDown() is called
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
        System.out.println(this.webDriver.getTitle());
        // TODO Auto-generated method stub

    }
}

But then, its getting into an infinite loop. The key press (space) is not being detected

Anirudh
  • 2,286
  • 4
  • 38
  • 64
praneel
  • 1,842
  • 4
  • 19
  • 24
  • What are you tryign to achieve? – smk Aug 16 '13 at 05:44
  • I am trying to use Automation to demo product features – praneel Aug 16 '13 at 05:58
  • 1
    how about running the tests in debug mode of your IDE and control the execution?? – Akbar Aug 16 '13 at 06:44
  • @TestAutomationEngr - You should post this as an answer, with a fuller description of how it solves the OP's problem. – Vince Bowdren Aug 16 '13 at 07:11
  • @TestAutomationEngr - I am looking for a solution which can be run from command line. Using IDE & breakpoints would be a very cumbersome process. Lets say, you are demo-ing a test which has 20 pages to navigate. – praneel Aug 16 '13 at 07:45
  • Agreed Praneel. I dont see any better readily available solution for your need. But if you do find anything, please post here as well. – Akbar Aug 16 '13 at 08:53

2 Answers2

0

I'm pretty sure what you want to achieve cannot be directly supported by Selenium. But i can share something we did for one of our projects.,we used python to simulate keyboard input for Android devices.

However, you could write some wrapper code in Python which waits for keyboard input and then s executes your Selenium code.

More info in this SO answer how to simulate keyboard input Which is the easiest way to simulate keyboard and mouse on Python?

Community
  • 1
  • 1
smk
  • 5,340
  • 5
  • 27
  • 41
  • But adding a wrapper would just take care of triggering the test but not really step into the test on keyboard interrupt – praneel Aug 16 '13 at 05:59
0

I was able to accomplish by the pointers provided in Krishnan Mahadevan's blog - https://gist.github.com/krmahadevan/1728633

Here is a snippet of it -

public EventFiringWebDriver initateWebDriverWithListener(){
    driver1 = new FirefoxDriver();
    EventFiringWebDriver driver = new EventFiringWebDriver(driver1);
    WebDriverListener eventListener = new WebDriverListener(driver);
    driver.register(eventListener);
}



public
class WebDriverListener implements WebDriverEventListener {
    public void afterNavigateTo(String url, WebDriver driver) {
        Logger.debug("Hit return ....");
        System.out.println("Hit return ....");
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Logger.debug("Proceeding further");
        System.out.println("Proceeding further");
    }
}

public class RunFromCommandLine{
    public static void main(String[] args) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        TestNG testng = new TestNG(); 
        testng.setXmlSuites((List <XmlSuite>)(new Parser("src"+File.separator+"test"+File.separator+"resources"+File.separator+"xml_Suites"+File.separator+"TestNG.xml").parse()));     
        testng.setSuiteThreadPoolSize(1);
        testng.run();
    }
}

maven command - mvn -X -P runClass clean test -DskipTests exec:java -Dexec.mainClass="com.shn.test.RunFromCommandLine" -Dexec.classpathScope=test -e

I am prompted for hitting the return key for every URL redirection that happens. On hitting any key, the test proceeds further.

praneel
  • 1,842
  • 4
  • 19
  • 24