{"id":18260486,"url":"https://github.com/sudharsan-selvaraj/spydriver","last_synced_at":"2025-04-04T19:33:20.365Z","repository":{"id":49649082,"uuid":"367124414","full_name":"sudharsan-selvaraj/spydriver","owner":"sudharsan-selvaraj","description":"🕵️  Lightweight utility to intercept WebDriver and WebElement method calls.","archived":false,"fork":false,"pushed_at":"2021-06-12T05:11:22.000Z","size":20325,"stargazers_count":29,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T20:51:36.403Z","etag":null,"topics":["automation-test","driver","interceptor","listener","mock","selenium","selenium-webdriver","spy","webdriver","webelement"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sudharsan-selvaraj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-13T17:28:01.000Z","updated_at":"2024-11-20T02:51:15.000Z","dependencies_parsed_at":"2022-09-05T06:01:30.197Z","dependency_job_id":null,"html_url":"https://github.com/sudharsan-selvaraj/spydriver","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudharsan-selvaraj%2Fspydriver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudharsan-selvaraj%2Fspydriver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudharsan-selvaraj%2Fspydriver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudharsan-selvaraj%2Fspydriver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sudharsan-selvaraj","download_url":"https://codeload.github.com/sudharsan-selvaraj/spydriver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247238448,"owners_count":20906445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation-test","driver","interceptor","listener","mock","selenium","selenium-webdriver","spy","webdriver","webelement"],"created_at":"2024-11-05T10:45:38.359Z","updated_at":"2025-04-04T19:33:15.357Z","avatar_url":"https://github.com/sudharsan-selvaraj.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"./assets/icon.svg\" height=150/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n   \u003ci\u003e\u003cstrong\u003eLightweight utility to intercept webdriver and webelement method calls. Supports both Selenium and Appium drivers \u003c/strong\u003e\u003c/i\u003e\n\u003cp\u003e\n \n### \u003cp align=\"center\"\u003e [About](#about) **|** [To Get Started](#how) **|** [Installation](#installation) \u003c/p\u003e\n\n---\n\n## About\n\nSpydriver is a utility library which helps in intercepting all webdriver and webelement method calls and enables us to\nperform any operation before and after the method is executed.\n\u003cp\u003e\n  \u003cimg src=\"./assets/recording_qtp.gif\" alt=\"spydriver.gif\"/\u003e\n\u003c/p\u003e\n\n## How?:\n\n### Creating the listener object:\n\n```java\n SpyDriverListener listener = new SpyDriverListener() {\n\n        @Override\n        public void beforeDriverCommandExecuted(DriverCommand command) {\n            System.out.println(\"[webdriver] Before \" + command.getMethod().getName() +\" =\u003e id: \"+ command.getId());\n            //Perform any action\n        }\n        \n        @Override\n        public void afterDriverCommandExecuted(DriverCommandResult command) {\n            System.out.println(\"[webdriver] After \" + command.getMethod().getName() +\" =\u003e id: \"+ command.getId());\n            //Perform any action\n        }\n        \n        @Override\n        public void onException(DriverCommandException command) {\n             //Perform any action\n        }\n        \n        @Override\n        public void beforeElementCommandExecuted(ElementCommand command) {\n            System.out.println(\"[webelement] Before \" + command.getMethod().getName() +\" =\u003e id: \"+ command.getId());\n            //Perform any action\n        }\n        \n        @Override\n        public void afterElementCommandExecuted(ElementCommandResult command) {\n            System.out.println(\"[webelement] After \" + command.getMethod().getName() +\" =\u003e id: \"+ command.getId());\n            //Perform any action\n        }\n        \n        @Override\n        public void onException(ElementCommandException command) {\n            //Perform any action\n        }\n    }\n```\n\n### Creating the spy driver object:\n\n```java\nSpyDriver spyDriver = new SpyDriver(new FirefoxDriver(), //Any webdriver object(supports both Selenium and Appium)\n        SpyDriverOptions.builder().listener(listener).build());// Listener that we have created above\nWebDriver driver = spyDriver.getSpyDriver();\n//now the driver object can be used in the test to get the events via listeners\n```\nIt's also possible to add any number of listeners later using\n```java\nspyDriver.addListener(listener);\n```\n\n\nThat's it. Now you can use the driver object in your test and it will log each and every method name that is invoked in the driver object. \n\n### Example:\n\n```java\ndriver.get(\"https://the-internet.herokuapp.com/\");\ndriver.manage().window().setSize(new Dimension(1000, 400));\ndriver.findElement(By.partialLinkText(\"Inputs\")).click();\ndriver.findElement(By.cssSelector(\"[type=\\\"number\\\"]\")).sendKeys(\"1222\");\ndriver.quit();\n```\n\n### Output:\n```shell\n[webdriver] Before get =\u003e id: a1275da9-f10b-4ab6-a268-3b963b83dc4a\n[webdriver] After get =\u003e id: a1275da9-f10b-4ab6-a268-3b963b83dc4a\n[webdriver] Before setSize =\u003e id: 4aff25ac-8c30-496b-a299-76a4bd1b0dbe\n[webdriver] After setSize =\u003e id: 4aff25ac-8c30-496b-a299-76a4bd1b0dbe\n[webdriver] Before findElement =\u003e id: 9d7e9167-6f5b-4f13-9f29-66c2e0239889\n[webdriver] After findElement =\u003e id: 9d7e9167-6f5b-4f13-9f29-66c2e0239889\n[webelement] Before click =\u003e id: 57247a4e-5be9-4af3-b9fa-c9142bacce67\n[webelement] After click =\u003e id: 57247a4e-5be9-4af3-b9fa-c9142bacce67\n[webdriver] Before findElement =\u003e id: 8d57434c-6c02-4e41-934f-86771fce9226\n[webdriver] After findElement =\u003e id: 8d57434c-6c02-4e41-934f-86771fce9226\n[webelement] Before sendKeys =\u003e id: 0233e88f-6e89-47a0-aa60-a3f3b94319c9\n[webelement] After sendKeys =\u003e id: 0233e88f-6e89-47a0-aa60-a3f3b94319c9\n[webdriver] Before quit =\u003e id: aa31341e-c464-4789-98f2-bb0aaa5cec87\n[webdriver] After quit =\u003e id: aa31341e-c464-4789-98f2-bb0aaa5cec87\n```\n\nCheck [SpyDriverExample.java](/src/test/java/examples/SpyDriverExample.java) for more usage.\n\n## Hooks:\n\n### beforeDriverCommandExecuted:\nInvoked before the webdriver method is called.\nParameter: `DriverCommand`\n```java\ndriver.get(\"https://www.google.com\");\n\npublic void beforeDriverCommandExecuted(DriverCommand command) {\n    System.out.println(command.getMethod().getName()); // prints \"get\"\n    System.out.println(command.getArguments()[0]); // prints \"https://www.google.com\"\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n    \n    Thread.sleep(2000); //Wait 2s before calling the get method\n    //You can add any cutom logic here in blocking mode   \n}\n```\n\n### afterDriverCommandExecuted:\nInvoked after the webdriver method is called. It also holds the return value from the original method call.\n\nParameter: `DriverCommandResult`\n```java\n//sample driver method\ndriver.getTitle();\n\npublic void afterDriverCommandExecuted(DriverCommandResult command) {\n    System.out.println(command.getMethod().getName()); // prints \"getTitle\"\n    System.out.println(command.getArguments()); // prints \"[]\" (because there are no parameters)\n    System.out.println(command.getResult()); // prints \"Google\"\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n}\n```\n\n```java\n//sample driver method\ndriver.findElement(By.cssSelector(\"body\"));\n\npublic void afterDriverCommandExecuted(DriverCommandResult command) {\n    System.out.println(command.getMethod().getName()); // prints \"findElement\"\n    By locator = (By) command.getArguments()[0];\n    WebElement locatedElement = (WebElement) command.getResult();\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n}\n```\n\n```java\n//sample driver method\ndriver.manage().window().getSize();\n\npublic void afterDriverCommandExecuted(DriverCommandResult command) {\n    System.out.println(command.getMethod().getName()); // prints \"getSize\"\n    System.out.println(command.getArguments()); // prints \"[]\" (because there are no parameters)\n    Dimension size = (Dimension) command.getResult(); // returns the current window size\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n}\n```\n\n### onException:\nInvoked if any exception is thrown while calling the webdriver method.\n\nParameter: `DriverCommandException`\n```java\n//sample driver method\ndriver.get(\"some-invalid-url\");\n\npublic void onException(DriverCommandException command) {\n    System.out.println(command.getMethod().get()); // prints \"get\"\n    System.out.println(command.getArguments()[0]); // prints \"some-invalid-url\"\n    System.out.println(command.getException().getMessage()); // Malformed URL: URL constructor: some-invalid-url is not a valid URL.\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n}\n```\n\n### beforeElementCommandExecuted:\nInvoked before any method is called in WebElement object.\n\nParameter: `ElementCommand`\n\n```java\n//sample driver method\nWebElement element = driver.findElement(By.cssSelector(\".username\"));\nelement.sendKeys(\"Test\", \" \", \"Ninja\");\n\npublic void beforeElementCommandExecuted(ElementCommand command) {\n    System.out.println(command.getMethod().getName()); // prints \"sendKeys\"\n    System.out.println(command.getArguments()[0]); // prints [\"Test\", \" \", \"Ninja\"]\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n    Webdriver originalElement = command.getElement(); // return the original element object that is being spied.\n    By locator = command.getLocator(); // returns the locator used to find the original element (By.cssSelector(\".username\")).\n}\n```\n\n```java\n//sample driver method\nWebElement element = driver.findElement(By.cssSelector(\".username\"));\nelement.click();\n\npublic void beforeElementCommandExecuted(ElementCommand command) {\n    System.out.println(command.getMethod().getName()); // prints \"click\"\n    System.out.println(command.getArguments()); // prints \"[]\"\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n    Webdriver originalElement = command.getElement(); // return the original element object that is being spied.\n    By locator = command.getLocator(); // returns the locator used to find the original element (By.cssSelector(\".username\")).\n}\n```\n\n### afterElementCommandExecuted:\nInvoked after any method is called in WebElement object.\n\nParameter: `ElementCommandResult`\n```java\n//sample driver method\nWebElement element = driver.findElement(By.cssSelector(\".username\"));\nString username = element.getAttribute(\"value\");\n\npublic void afterElementCommandExecuted(ElementCommandResult command) {\n    System.out.println(command.getMethod().getName()); // prints \"getAttribute\"\n    System.out.println(command.getArguments()[0]); // prints [\"value\"]\n    System.out.println(command.getResult()); // prints \"Test Ninaje\"\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n    Webdriver originalElement = command.getElement(); // return the original element object that is being spied.\n    By locator = command.getLocator(); // returns the locator used to find the original element (By.cssSelector(\".username\"))\n}\n```\n\n### onException:\nInvoked if any exception is thrown while calling the webelement method.\n\nParameter: `ElementCommandException`\n```java\n//sample driver method\nWebElement element = driver.findElement(By.cssSelector(\".hidden\"));\nelement.click();\n\npublic void onException(ElementCommandException command) {\n    System.out.println(command.getMethod().getName()); // prints \"click\"\n    System.out.println(command.getException().getMessage()); // prints \"element not visile\"\n    Webdriver originalDriver = command.getDriver(); // return the original driver object that is being spied.\n    Webdriver originalElement = command.getElement(); // return the original element object that is being spied.\n    By locator = command.getLocator(); // returns the locator used to find the original element (By.cssSelector(\".hidden\"))\n}\n```\n\n## Installation:\n\n### Maven:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eio.github.sudharsan-selvaraj\u003c/groupId\u003e\n  \u003cartifactId\u003espydriver\u003c/artifactId\u003e\n  \u003cversion\u003e1.1.1\u003c/version\u003e\n\u003c/dependency\u003e \n```\n\n### Gradle:\n\n```groovy\nimplementation group: 'io.github.sudharsan-selvaraj', name: 'spydriver', version: '1.1.1'\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudharsan-selvaraj%2Fspydriver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsudharsan-selvaraj%2Fspydriver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudharsan-selvaraj%2Fspydriver/lists"}