Best Selenium code snippet using org.openqa.selenium.remote.Interface DriverCommand.ACTIONS
Source:CraftDriver.java
1package com.cognizant.framework.selenium;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MultiTouchAction;4import io.appium.java_client.TouchAction;5import java.net.URL;6import java.util.List;7import java.util.Map;8import java.util.Set;9import java.util.logging.Level;10import org.openqa.selenium.By;11import org.openqa.selenium.Capabilities;12//import org.openqa.selenium.OutputType;13import org.openqa.selenium.ScreenOrientation;14import org.openqa.selenium.TimeoutException;15import org.openqa.selenium.WebDriver;16import org.openqa.selenium.WebDriver.Navigation;17import org.openqa.selenium.WebDriver.Options;18import org.openqa.selenium.WebDriver.TargetLocator;19import org.openqa.selenium.html5.Location;20import org.openqa.selenium.interactions.Actions;21import org.openqa.selenium.interactions.Keyboard;22import org.openqa.selenium.interactions.Mouse;23import org.openqa.selenium.remote.CommandExecutor;24import org.openqa.selenium.remote.ErrorHandler;25import org.openqa.selenium.remote.ExecuteMethod;26import org.openqa.selenium.remote.FileDetector;27import org.openqa.selenium.remote.Response;28import org.openqa.selenium.remote.SessionId;29import org.openqa.selenium.support.ui.ExpectedConditions;30import org.openqa.selenium.support.ui.WebDriverWait;31import org.openqa.selenium.WebElement;32import com.cognizant.framework.Status;33import com.experitest.client.Client;34import com.experitest.selenium.MobileDevice;35import com.experitest.selenium.MobileWebDriver;36import com.google.gson.JsonObject;37public class CraftDriver {38 private SeleniumTestParameters testParameters;39 private SeleniumReport report;40 private WebDriver driver;41 private MobileWebDriver seeTestDriver;42 public CraftDriver(WebDriver driver) {43 this.driver = driver;44 }45 public SeleniumTestParameters getTestParameters() {46 return testParameters;47 }48 public void setTestParameters(SeleniumTestParameters testParameters) {49 this.testParameters = testParameters;50 }51 public void setRport(SeleniumReport report) {52 this.report = report;53 }54 public WebDriver getWebDriver() {55 return driver;56 }57 public void setSeeTestDriver(MobileWebDriver seeTestDriver) {58 this.seeTestDriver = seeTestDriver;59 }60 public MobileWebDriver getSeeTestDriver() {61 return seeTestDriver;62 }63 public boolean isPerfecto() {64 boolean isPerfecto = false;65 if (testParameters.getExecutionMode().toString().equals("PERFECTO")) {66 isPerfecto = true;67 }68 return isPerfecto;69 }70 public boolean isAppium() {71 boolean isAppium = false;72 if (testParameters.getMobileToolName().toString().equals("APPIUM")) {73 isAppium = true;74 }75 return isAppium;76 }77 public boolean isDefaultPerfecto() {78 boolean isDefaultPerfecto = false;79 if (testParameters.getMobileToolName().toString().equals("DEFAULT")) {80 isDefaultPerfecto = true;81 }82 return isDefaultPerfecto;83 }84 public boolean isSeeTest() {85 boolean isSeeTest = false;86 if (testParameters.getExecutionMode().toString().equals("SEETEST")) {87 isSeeTest = true;88 }89 return isSeeTest;90 }91 // WebDriver Methods92 /**93 * Function to close the driver Object {@link WebDriver}94 */95 public void close() {96 driver.close();97 }98 /**99 * Function to identity the Element100 * 101 * @param by102 * The locator used to identify the element {@link WebDriver}103 */104 public boolean equals(Object obj) {105 return driver.equals(obj);106 }107 /**108 * Function to Find the first {@link WebElement} using the given method.109 * 110 * @param by111 * The locator used to identify the element {@link WebDriver}112 */113 public WebElement findElement(By arg0) {114 return driver.findElement(arg0);115 }116 /**117 * Function to Find the first {@link WebElement}, also take screen shot and118 * wait until the specified element is visible119 * 120 * @param by121 * The locator used to identify the element {@link WebDriver}122 */123 public WebElement findElementnTakescreenShot(By arg0) {124 WebElement element;125 if (driver.findElement(arg0).isDisplayed() || isElementVisible(arg0)) {126 report.updateTestLog("Action", "Action Performed Successfully",127 Status.PASS);128 element = driver.findElement(arg0);129 } else {130 report.updateTestLog("Action",131 "Action Failed to Perform, please check the Locators",132 Status.FAIL);133 element = null;134 }135 return element;136 }137 /**138 * Function to wait until the specified element is visible139 * 140 * @param by141 * The locator used to identify the element {@link WebDriver}142 */143 public boolean isElementVisible(By arg0) {144 boolean elementVisible = false;145 try {146 (new WebDriverWait(driver, 60)).until(ExpectedConditions147 .visibilityOfElementLocated(arg0));148 elementVisible = true;149 } catch (TimeoutException ex) {150 elementVisible = false;151 }152 return elementVisible;153 }154 /**155 * Function to take screen shot and update in report {@link WebDriver}156 */157 public void capture() {158 report.updateTestLog("Screen Shot", "Screen Shot Captured Successfuly",159 Status.SCREENSHOT);160 }161 /**162 * Function to Find all elements within the current page using the given163 * mechanism164 * 165 * @param by166 * The locator used to identify the list of elements167 * {@link WebDriver}168 */169 public List<WebElement> findElements(By arg0) {170 return driver.findElements(arg0);171 }172 /**173 * Function to Load a new web page in the current browser window.174 * {@link WebDriver}175 */176 public void get(String arg0) {177 driver.get(arg0);178 }179 public Class<?> getClass_Driver() {180 return driver.getClass();181 }182 /**183 * Function to Get a string representing the current URL that the browser is184 * looking at. {@link WebDriver}185 */186 public String getCurrentUrl() {187 return driver.getCurrentUrl();188 }189 /**190 * Function to Get the source of the last loaded page. {@link WebDriver}191 */192 public String getPageSource() {193 return driver.getPageSource();194 }195 /**196 * Function to get The title of the current page. {@link WebDriver}197 */198 public String getTitle() {199 return driver.getTitle();200 }201 /**202 * Function to Return an opaque handle to this window that uniquely203 * identifies it within this driver instance {@link WebDriver}204 */205 public String getWindowHandle() {206 return driver.getWindowHandle();207 }208 /**209 * Function to Return a set of window handles which can be used to iterate210 * over all open windows of this WebDriver instance by passing them to211 * {@link switchTo} {@link WebDriver}212 */213 public Set<String> getWindowHandles() {214 return driver.getWindowHandles();215 }216 public int hashCode() {217 return driver.hashCode();218 }219 /**220 * Function to Gets the Option interface. {@link WebDriver}221 */222 public Options manage() {223 return driver.manage();224 }225 /**226 * Function to GetAn abstraction allowing the driver to access the browser's227 * history and to navigate to a given URL. {@link WebDriver}228 */229 public Navigation navigate() {230 return driver.navigate();231 }232 public void notify_Driver() {233 driver.notify();234 }235 public void notifyAll_Driver() {236 driver.notifyAll();237 }238 /**239 * Function to Quit this driver, closing every associated window..240 * {@link WebDriver}241 */242 public void quit() {243 driver.quit();244 }245 /**246 * Function to Send future commands to a different frame or window.247 * {@link WebDriver}248 */249 public TargetLocator switchTo() {250 return driver.switchTo();251 }252 public String toString() {253 return driver.toString();254 }255 public void wait_Driver() throws InterruptedException {256 driver.wait();257 }258 public void wait_Driver(long timeout) throws InterruptedException {259 driver.wait(timeout);260 }261 public void wait_Driver(long timeout, int nanos)262 throws InterruptedException {263 driver.wait(timeout, nanos);264 }265 // Perfecto Scripts266 /**267 * Function Applicable only when the tool used is <b>APPIUM i.e.,268 * {@link AppiumDriver}.269 */270 @SuppressWarnings("rawtypes")271 public Object executeAsyncScript(String arg0, Object... arg1) {272 if (isAppium()) {273 return ((AppiumDriver) driver).executeAsyncScript(arg0, arg1);274 } else {275 return null;276 }277 }278 /**279 * Function Applicable only when the tool used is <b>APPIUM i.e.,280 * {@link AppiumDriver}.281 */282 @SuppressWarnings("rawtypes")283 public Object executeScript(String arg0, Object... arg1) {284 if (isAppium()) {285 return ((AppiumDriver) driver).executeScript(arg0, arg1);286 } else {287 return null;288 }289 }290 /**291 * Function Applicable only when the tool used is <b>APPIUM i.e.,292 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.293 */294 @SuppressWarnings("rawtypes")295 public WebElement findElementById(String arg0) {296 if (isSeeTest()) {297 return ((MobileWebDriver) driver).findElementById(arg0);298 } else if (isAppium()) {299 return ((AppiumDriver) driver).findElementById(arg0);300 } else {301 return null;302 }303 }304 /**305 * Function Applicable only when the tool used is OR <b>APPIUM i.e.,306 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.307 */308 @SuppressWarnings({ "unchecked", "rawtypes" })309 public List<WebElement> findElementsById(String arg0) {310 if (isSeeTest()) {311 return ((MobileWebDriver) driver).findElementsById(arg0);312 } else if (isAppium()) {313 return ((AppiumDriver) driver).findElementsById(arg0);314 } else {315 return null;316 }317 }318 /**319 * Function Applicable only when the tool used is <b>APPIUM i.e.,320 * {@link AppiumDriver}.321 */322 @SuppressWarnings("rawtypes")323 public WebElement findElementByName(String arg0) {324 if (isAppium()) {325 return ((AppiumDriver) driver).findElementByName(arg0);326 } else {327 return null;328 }329 }330 /**331 * Function Applicable only when the tool used is OR <b>APPIUM i.e.,332 * {@link AppiumDriver}.333 */334 @SuppressWarnings({ "unchecked", "rawtypes" })335 public List<WebElement> findElementsByName(String arg0) {336 if (isAppium()) {337 return ((AppiumDriver) driver).findElementsByName(arg0);338 } else {339 return null;340 }341 }342 /**343 * Function Applicable only when the tool used is <b>APPIUM i.e.,344 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.345 */346 @SuppressWarnings("rawtypes")347 public WebElement findElementByXPath(String arg0) {348 if (isSeeTest()) {349 return ((MobileWebDriver) driver).findElementByXPath(arg0);350 } else if (isAppium()) {351 return ((AppiumDriver) driver).findElementByXPath(arg0);352 } else {353 return null;354 }355 }356 /**357 * Function Applicable only when the tool used is <b>APPIUM i.e.,358 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.359 */360 @SuppressWarnings({ "unchecked", "rawtypes" })361 public List<WebElement> findElementsByXPath(String arg0) {362 if (isSeeTest()) {363 return ((MobileWebDriver) driver).findElementsByXPath(arg0);364 } else if (isAppium()) {365 return ((AppiumDriver) driver).findElementsByXPath(arg0);366 } else {367 return null;368 }369 }370 /**371 * Function Applicable only when the tool used is < <b>APPIUM i.e.,372 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.373 */374 @SuppressWarnings("rawtypes")375 public WebElement findElementByLinkText(String arg0) {376 if (isSeeTest()) {377 return ((MobileWebDriver) driver).findElementByLinkText(arg0);378 } else if (isAppium()) {379 return ((AppiumDriver) driver).findElementByLinkText(arg0);380 } else {381 return null;382 }383 }384 /**385 * Function Applicable only when the tool used is <b>APPIUM i.e.,386 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.387 */388 @SuppressWarnings({ "unchecked", "rawtypes" })389 public List<WebElement> findElementsByLinkText(String arg0) {390 if (isSeeTest()) {391 return ((MobileWebDriver) driver).findElementsByLinkText(arg0);392 } else if (isAppium()) {393 return ((AppiumDriver) driver).findElementsByLinkText(arg0);394 } else {395 return null;396 }397 }398 /**399 * Function Applicable only when the tool used is <b>APPIUM i.e.,400 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.401 */402 @SuppressWarnings("rawtypes")403 public WebElement findElementByPartialLinkText(String arg0) {404 if (isSeeTest()) {405 return ((MobileWebDriver) driver)406 .findElementByPartialLinkText(arg0);407 } else if (isAppium()) {408 return ((AppiumDriver) driver).findElementByPartialLinkText(arg0);409 } else {410 return null;411 }412 }413 /**414 * Function Applicable only when the tool used is <b>APPIUM i.e.,415 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.416 */417 @SuppressWarnings({ "unchecked", "rawtypes" })418 public List<WebElement> findElementsByPartialLinkText(String arg0) {419 if (isSeeTest()) {420 return ((MobileWebDriver) driver)421 .findElementsByPartialLinkText(arg0);422 } else if (isAppium()) {423 return ((AppiumDriver) driver).findElementsByPartialLinkText(arg0);424 } else {425 return null;426 }427 }428 /**429 * Function Applicable only when the tool used is <b>APPIUM i.e.,430 * {@link AppiumDriver}.431 */432 @SuppressWarnings("rawtypes")433 public WebElement findElementByClassName(String arg0) {434 if (isAppium()) {435 return ((AppiumDriver) driver).findElementByClassName(arg0);436 } else {437 return null;438 }439 }440 /**441 * Function Applicable only when the tool used is <b>APPIUM i.e.,442 * {@link AppiumDriver} OR <b>SEETEST i.e., {@link MobileWebDriver}.443 */444 @SuppressWarnings({ "unchecked", "rawtypes" })445 public List<WebElement> findElementsByClassName(String arg0) {446 if (isAppium()) {447 return ((AppiumDriver) driver).findElementsByClassName(arg0);448 } else if (isSeeTest()) {449 return ((MobileWebDriver) driver).findElementsByClassName(arg0);450 } else {451 return null;452 }453 }454 /**455 * Function Applicable only when the tool used is <b>APPIUM i.e.,456 * {@link AppiumDriver}.457 */458 @SuppressWarnings("rawtypes")459 public WebElement findElementByTagName(String arg0) {460 if (isAppium()) {461 return ((AppiumDriver) driver).findElementByTagName(arg0);462 } else {463 return null;464 }465 }466 /**467 * Function Applicable only when the tool used is <b>APPIUM i.e.,468 * {@link AppiumDriver}.469 */470 @SuppressWarnings({ "rawtypes", "unchecked" })471 public List<WebElement> findElementsByTagName(String arg0) {472 if (isAppium()) {473 return ((AppiumDriver) driver).findElementsByTagName(arg0);474 } else {475 return null;476 }477 }478 /**479 * Function Applicable only when the tool used is <b>APPIUM i.e.,480 * {@link AppiumDriver}.481 */482 @SuppressWarnings("rawtypes")483 public WebElement findElementByCssSelector(String arg0) {484 if (isAppium()) {485 return ((AppiumDriver) driver).findElementByCssSelector(arg0);486 } else {487 return null;488 }489 }490 /**491 * Function Applicable only when the tool used is <b>APPIUM i.e.,492 * {@link AppiumDriver}.493 */494 @SuppressWarnings({ "unchecked", "rawtypes" })495 public List<WebElement> findElementsByCssSelector(String arg0) {496 if (isAppium()) {497 return ((AppiumDriver) driver).findElementsByCssSelector(arg0);498 } else {499 return null;500 }501 }502 // Appium Methods503 /**504 * Function Applicable only when the tool used is <b>APPIUM i.e.,505 * {@link AppiumDriver}.506 */507 @SuppressWarnings("rawtypes")508 public Capabilities getCapabilities() {509 return ((AppiumDriver) driver).getCapabilities();510 }511 /**512 * Function Applicable only when the tool used is <b>APPIUM i.e.,513 * {@link AppiumDriver}.514 */515 @SuppressWarnings("rawtypes")516 public CommandExecutor getCommandExecutor() {517 return ((AppiumDriver) driver).getCommandExecutor();518 }519 /**520 * Function Applicable only when the tool used is <b>APPIUM i.e.,521 * {@link AppiumDriver}.522 */523 @SuppressWarnings("rawtypes")524 public ErrorHandler getErrorHandler() {525 return ((AppiumDriver) driver).getErrorHandler();526 }527 /**528 * Function Applicable only when the tool used is <b>APPIUM i.e.,529 * {@link AppiumDriver}.530 */531 @SuppressWarnings("rawtypes")532 public ExecuteMethod getExecuteMethod() {533 return ((AppiumDriver) driver).getExecuteMethod();534 }535 /**536 * Function Applicable only when the tool used is <b>APPIUM i.e.,537 * {@link AppiumDriver}.538 */539 @SuppressWarnings("rawtypes")540 public FileDetector getFileDetector() {541 return ((AppiumDriver) driver).getFileDetector();542 }543 /**544 * Function Applicable only when the tool used is <b>APPIUM i.e.,545 * {@link AppiumDriver}.546 */547 @SuppressWarnings("rawtypes")548 public Keyboard getKeyboard() {549 return ((AppiumDriver) driver).getKeyboard();550 }551 /**552 * Function Applicable only when the tool used is <b>APPIUM i.e.,553 * {@link AppiumDriver}.554 */555 @SuppressWarnings({ "rawtypes", "deprecation" })556 public Mouse getMouse() {557 return ((AppiumDriver) driver).getMouse();558 }559 /**560 * Function Applicable only when the tool used is <b>APPIUM i.e.,561 * {@link AppiumDriver}.562 */563 @SuppressWarnings("rawtypes")564 public WebDriver context(String arg0) {565 return ((AppiumDriver) driver).context(arg0);566 }567 /**568 * Function Applicable only when the tool used is <b>APPIUM i.e.,569 * {@link AppiumDriver}.570 */571 @SuppressWarnings({ "rawtypes", "unchecked" })572 public Response execute(String driverCommand, Map<String, ?> parameters) {573 return ((AppiumDriver) driver).execute(driverCommand, parameters);574 }575 /**576 * Function Applicable only when the tool used is <b>APPIUM i.e.,577 * {@link AppiumDriver}.578 */579 @SuppressWarnings("rawtypes")580 public void performMultiTouchAction(MultiTouchAction arg0) {581 ((AppiumDriver) driver).performMultiTouchAction(arg0);582 }583 /**584 * Function Applicable only when the tool used is <b>APPIUM i.e.,585 * {@link AppiumDriver}.586 */587 @SuppressWarnings("rawtypes")588 public TouchAction performTouchAction(TouchAction arg0) {589 return ((AppiumDriver) driver).performTouchAction(arg0);590 }591 /**592 * Function Applicable only when the tool used is <b>APPIUM i.e.,593 * {@link AppiumDriver}.594 */595 @SuppressWarnings("rawtypes")596 public String getContext() {597 return ((AppiumDriver) driver).getContext();598 }599 /**600 * Function Applicable only when the tool used is <b>APPIUM i.e.,601 * {@link AppiumDriver}.602 */603 @SuppressWarnings({ "rawtypes", "unchecked" })604 public Set<String> getContextHandles() {605 return ((AppiumDriver) driver).getContextHandles();606 }607 /**608 * Function Applicable only when the tool used is <b>APPIUM i.e.,609 * {@link AppiumDriver}.610 */611 @SuppressWarnings("rawtypes")612 public ScreenOrientation getOrientation() {613 return ((AppiumDriver) driver).getOrientation();614 }615 /**616 * Function Applicable only when the tool used is <b>APPIUM i.e.,617 * {@link AppiumDriver}.618 */619 @SuppressWarnings("rawtypes")620 public URL getRemoteAddress() {621 return ((AppiumDriver) driver).getRemoteAddress();622 }623 // public X getScreenshotAs(OutputType<X>outputType){624 //625 // }626 /**627 * Function Applicable only when the tool used is <b>APPIUM i.e.,628 * {@link AppiumDriver}.629 */630 @SuppressWarnings("rawtypes")631 public SessionId getSessionId() {632 return ((AppiumDriver) driver).getSessionId();633 }634 /**635 * Function Applicable only when the tool used is <b>APPIUM i.e.,636 * {@link AppiumDriver}.637 */638 @SuppressWarnings("rawtypes")639 public JsonObject getSetting() {640 return ((AppiumDriver) driver).getSettings();641 }642 // @SuppressWarnings("rawtypes")643 // public int getW3StandardComplainaceLevel() {644 // return ((AppiumDriver) driver).getW3CStandardComplianceLevel();645 // }646 /**647 * Function Applicable only when the tool used is <b>APPIUM i.e.,648 * {@link AppiumDriver}.649 */650 @SuppressWarnings("rawtypes")651 public void rotate(ScreenOrientation arg0) {652 ((AppiumDriver) driver).rotate(arg0);653 }654 /**655 * Function Applicable only when the tool used is <b>APPIUM i.e.,656 * {@link AppiumDriver}.657 */658 @SuppressWarnings("rawtypes")659 public WebElement findElementByAccessibilityId(String arg0) {660 return ((AppiumDriver) driver).findElementByAccessibilityId(arg0);661 }662 /**663 * Function Applicable only when the tool used is <b>APPIUM i.e.,664 * {@link AppiumDriver}.665 */666 @SuppressWarnings({ "rawtypes", "unchecked" })667 public List<WebElement> findElementsByAccessibilityId(String arg0) {668 return ((AppiumDriver) driver).findElementsByAccessibilityId(arg0);669 }670 /**671 * Function Applicable only when the tool used is <b>APPIUM i.e.,672 * {@link AppiumDriver}.673 */674 @SuppressWarnings("rawtypes")675 public Location location() {676 return ((AppiumDriver) driver).location();677 }678 // public int lockScreen(int seconds){679 // return ((AppiumDriver) driver).lockScreen(seconds);680 // }681 /**682 * Function Applicable only when the tool used is <b>APPIUM i.e.,683 * {@link AppiumDriver}.684 */685 @SuppressWarnings("rawtypes")686 public void setLocation(Location arg0) {687 ((AppiumDriver) driver).setLocation(arg0);688 }689 /**690 * Function Applicable only when the tool used is <b>APPIUM i.e.,691 * {@link AppiumDriver}.692 */693 @SuppressWarnings("rawtypes")694 public void hideKeyboard() {695 ((AppiumDriver) driver).hideKeyboard();696 }697 /**698 * Function Applicable only when the tool used is <b>APPIUM i.e.,699 * {@link AppiumDriver}.700 */701 @SuppressWarnings("rawtypes")702 public void pinch(WebElement arg0) {703 ((AppiumDriver) driver).pinch(arg0);704 }705 /**706 * Function Applicable only when the tool used is <b>APPIUM i.e.,707 * {@link AppiumDriver}.708 */709 @SuppressWarnings("rawtypes")710 public void pinch(int x, int y) {711 ((AppiumDriver) driver).pinch(x, y);712 }713 /**714 * Function Applicable only when the tool used is <b>APPIUM i.e.,715 * {@link AppiumDriver}.716 */717 @SuppressWarnings("rawtypes")718 public void setErrorHandler(ErrorHandler handler) {719 ((AppiumDriver) driver).setErrorHandler(handler);720 }721 /**722 * Function Applicable only when the tool used is <b>APPIUM i.e.,723 * {@link AppiumDriver}.724 */725 @SuppressWarnings("rawtypes")726 public void setFileDetector(FileDetector detector) {727 ((AppiumDriver) driver).setFileDetector(detector);728 }729 /**730 * Function Applicable only when the tool used is <b>APPIUM i.e.,731 * {@link AppiumDriver}.732 */733 @SuppressWarnings("rawtypes")734 public void setLogLevel(Level level) {735 ((AppiumDriver) driver).setLogLevel(level);736 }737 /**738 * Function Applicable only when the tool used is <b>APPIUM i.e.,739 * {@link AppiumDriver}.740 */741 @SuppressWarnings("rawtypes")742 public void swipe(int startx, int starty, int endx, int endy, int duration) {743 ((AppiumDriver) driver).swipe(startx, starty, endx, endy, duration);744 }745 /**746 * Function Applicable only when the tool used is <b>APPIUM i.e.,747 * {@link AppiumDriver}.748 */749 @SuppressWarnings("rawtypes")750 public void tap(int fingers, WebElement element, int duration) {751 ((AppiumDriver) driver).tap(fingers, element, duration);752 }753 /**754 * Function Applicable only when the tool used is <b>APPIUM i.e.,755 * {@link AppiumDriver}.756 */757 @SuppressWarnings("rawtypes")758 public void tap(int fingers, int x, int y, int duration) {759 ((AppiumDriver) driver).tap(fingers, x, y, duration);760 }761 /**762 * Function Applicable only when the tool used is <b>APPIUM i.e.,763 * {@link AppiumDriver}.764 */765 @SuppressWarnings("rawtypes")766 public void zoom(WebElement el) {767 ((AppiumDriver) driver).zoom(el);768 }769 /**770 * Function Applicable only when the tool used is <b>APPIUM i.e.,771 * {@link AppiumDriver}.772 */773 @SuppressWarnings("rawtypes")774 public void zoom(int x, int y) {775 ((AppiumDriver) driver).zoom(x, y);776 }777 /**778 * Function Applicable only when the tool used is <b>APPIUM i.e.,779 * {@link AppiumDriver}.780 */781 @SuppressWarnings("rawtypes")782 public byte[] pullFile(String remotePath) {783 return ((AppiumDriver) driver).pullFile(remotePath);784 }785 /**786 * Function Applicable only when the tool used is <b>APPIUM i.e.,787 * {@link AppiumDriver}.788 */789 @SuppressWarnings("rawtypes")790 public byte[] pullFolder(String remotePath) {791 return ((AppiumDriver) driver).pullFolder(remotePath);792 }793 /**794 * Function Applicable only when the tool used is <b>APPIUM i.e.,795 * {@link AppiumDriver}.796 */797 @SuppressWarnings("rawtypes")798 public void closeApp() {799 ((AppiumDriver) driver).closeApp();800 }801 /**802 * Function Applicable only when the tool used is <b>APPIUM i.e.,803 * {@link AppiumDriver}.804 */805 @SuppressWarnings("rawtypes")806 public void installApp(String appPath) {807 ((AppiumDriver) driver).installApp(appPath);808 }809 /**810 * Function Applicable only when the tool used is <b>APPIUM i.e.,811 * {@link AppiumDriver}.812 */813 @SuppressWarnings("rawtypes")814 public boolean isAppInstalled(String bundleId) {815 return ((AppiumDriver) driver).isAppInstalled(bundleId);816 }817 /**818 * Function Applicable only when the tool used is <b>APPIUM i.e.,819 * {@link AppiumDriver}.820 */821 @SuppressWarnings("rawtypes")822 public void launchApp() {823 ((AppiumDriver) driver).launchApp();824 }825 /**826 * Function Applicable only when the tool used is <b>APPIUM i.e.,827 * {@link AppiumDriver}.828 */829 @SuppressWarnings("rawtypes")830 public void removeApp(String bundleId) {831 ((AppiumDriver) driver).removeApp(bundleId);832 }833 /**834 * Function Applicable only when the tool used is <b>APPIUM i.e.,835 * {@link AppiumDriver}.836 */837 @SuppressWarnings("rawtypes")838 public void resetApp() {839 ((AppiumDriver) driver).resetApp();840 }841 /**842 * Function Applicable only when the tool used is <b>APPIUM i.e.,843 * {@link AppiumDriver}.844 */845 @SuppressWarnings("rawtypes")846 public void runAppInBackground(int seconds) {847 ((AppiumDriver) driver).runAppInBackground(seconds);848 }849 // seeTest Methods850 /**851 * Function Applicable only when the tool used is <b>SEETEST i.e.,852 * {@link MobileWebDriver}.853 */854 public Client client() {855 return ((MobileWebDriver) driver).client;856 }857 /**858 * Function Applicable only when the tool used is <b>SEETEST i.e.,859 * {@link MobileWebDriver}.860 */861 public void application(String appPath) {862 ((MobileWebDriver) driver).application(appPath);863 }864 /**865 * Function Applicable only when the tool used is <b>SEETEST i.e.,866 * {@link MobileWebDriver}.867 */868 public MobileDevice device() {869 return ((MobileWebDriver) driver).device();870 }871 /**872 * Function Applicable only when the tool used is <b>SEETEST i.e.,873 * {@link MobileWebDriver}.874 */875 public void generateReport() {876 ((MobileWebDriver) driver).generateReport();877 }878 /**879 * Function Applicable only when the tool used is <b>SEETEST i.e.,880 * {@link MobileWebDriver}.881 */882 public String getDeviceInformation() {883 return ((MobileWebDriver) driver).getDeviceInformation();884 }885 /**886 * Function Applicable only when the tool used is <b>SEETEST i.e.,887 * {@link MobileWebDriver}.888 */889 public MobileDevice setDevice(String deviceName) {890 return ((MobileWebDriver) driver).setDevice(deviceName);891 }892 /**893 * Function Applicable only when the tool used is <b>SEETEST i.e.,894 * {@link MobileWebDriver}.895 */896 public void startVideoRecord() {897 ((MobileWebDriver) driver).startVideoRecord();898 }899 /**900 * Function Applicable only when the tool used is <b>SEETEST i.e.,901 * {@link MobileWebDriver}.902 */903 public void stopVideoRecord() {904 ((MobileWebDriver) driver).stopVideoRecord();905 }906 /**907 * Function Applicable only when the tool used is <b>SEETEST i.e.,908 * {@link MobileWebDriver}.909 */910 public void useNativeIdentification() {911 ((MobileWebDriver) driver).useNativeIdentification();912 }913 /**914 * Function Applicable only when the tool used is <b>SEETEST i.e.,915 * {@link MobileWebDriver}.916 */917 public void useWebIdentification() {918 ((MobileWebDriver) driver).useWebIdentification();919 }920 /**921 * Function Applicable only when the tool used is <b>SEETEST i.e.,922 * {@link MobileWebDriver}.923 */924 public MobileDevice webForDevice(String deviceName, int timeout) {925 return ((MobileWebDriver) driver).webForDevice(deviceName, timeout);926 }927 928 public void pause(String timeUnitSec){929 try{930 Long delay=Long.parseLong(timeUnitSec)*1000;931 Thread.sleep(delay);932 }catch(Exception e){}933 }934 935 936 937}...
Source:Keyboard_Mouse_Actions.java
1package com.github.web.automation;23import java.awt.AWTException;4import java.awt.Robot;5import java.awt.Toolkit;6import java.awt.datatransfer.StringSelection;7import java.awt.event.KeyEvent;8import java.io.File;9import java.io.IOException;1011import org.apache.commons.io.FileUtils;12import org.openqa.selenium.By;13import org.openqa.selenium.Keys;14import org.openqa.selenium.WebDriverException;15import org.openqa.selenium.WebElement;16import org.openqa.selenium.interactions.Action;17import org.openqa.selenium.interactions.Actions;18import org.openqa.selenium.remote.LocalFileDetector;19import org.openqa.selenium.remote.RemoteWebElement;20import org.openqa.selenium.support.ui.ExpectedConditions;2122import com.github.web.automation.Verifications.ByType;2324import enums.ActionType;2526/**27 * <UL>All actions implement the 28 * <a href="https://github.com/SeleniumHQ/selenium/wiki/Advanced-User-Interactions"> Action interface.</a>29 * <LI>ClickAction - Equivalent to WebElement.click()</LI>30 * <LI>KeyDownAction - Holding down a modifier key.</LI>31 * <LI>KeyUpAction - Releasing a modifier key.</LI>32 * <LI>SendKeysAction - Equivalent to WebElement.sendKey(...)</LI>33 * <LI> ... </LI>34 * </UL>35 * 36 * <P>Element <a href="https://stackoverflow.com/a/14222645/5081877">Drag and Drop</a>37 * 38 * @author yashwanth.m39 *40 */41public class Keyboard_Mouse_Actions extends ScreenShot {42 43 /**44 * https://stackoverflow.com/a/46447150/508187745 * https://sqa.stackexchange.com/questions/12851/how-can-i-work-with-file-uploads-during-a-webdriver-test46 * 47 * @param locator48 * @param locatorType49 * @param filePath50 * @return51 */52 public boolean FileUpload(String locator, ByType locatorType, String filePath, ActionType type) {53 By findBy = findBy(locator, locatorType);54 WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));55 System.out.println("\t ===== FILE UPLOAD =====");56 if( type == ActionType.SEND_KEYS ) {57 try {58 element.sendKeys( filePath );59 if ( takeScreenShot ) {60 takeElementScreenshot(element);61 }62 return true;63 } catch (WebDriverException e) {64 // Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element65 System.err.println("File Upload SendKeys Exception : "+e.getMessage());66 if ( !e.getMessage().contains("cannot focus element") ) {67 return false;68 } else {69 type = ActionType.WIN;70 }71 }72 } else if ( type == ActionType.FILE_DETECTOR ) {73 LocalFileDetector detector = new LocalFileDetector();74 File localFile = detector.getLocalFile( filePath );75 RemoteWebElement input = (RemoteWebElement) driver.findElement(By.xpath(locator));76 input.setFileDetector(detector);77 input.sendKeys(localFile.getAbsolutePath());78 input.click();79 /*80 * String zip = new Zip().zipFile(localFile.getParentFile(), localFile);81 * Response response = execute(DriverCommand.UPLOAD_FILE, ImmutableMap.of("file", zip));82 * return (String) response.getValue();83 */84 return true;85 }86 try {87 element.click();88 Thread.sleep( 1000 * 2 );89 90 setClipboardData(filePath);91 92 Robot robot = new Robot();93 if( type == ActionType.MAC ) { // Apple's Unix-based operating system.94 95 // âGo To Folderâ on Mac - Hit Command+Shift+G on a Finder window.96 // http://osxdaily.com/2011/08/31/go-to-folder-useful-mac-os-x-keyboard-shortcut/97 robot.keyPress(KeyEvent.VK_META);98 robot.keyPress(KeyEvent.VK_SHIFT);99 robot.keyPress(KeyEvent.VK_G);100 robot.keyRelease(KeyEvent.VK_G);101 robot.keyRelease(KeyEvent.VK_SHIFT);102 robot.keyRelease(KeyEvent.VK_META);103104 // Paste the clipBoard content - Command â + V.105 robot.keyPress(KeyEvent.VK_META);106 robot.keyPress(KeyEvent.VK_V);107 robot.keyRelease(KeyEvent.VK_V);108 robot.keyRelease(KeyEvent.VK_META);109 110 // Press Enter (GO - To bring up the file.)111 robot.keyPress(KeyEvent.VK_ENTER);112 robot.keyRelease(KeyEvent.VK_ENTER);113 return true;114 } else if ( type == ActionType.WIN || type == ActionType.LINUX ) { // Ctrl + V to paste the content.115 // paste to the current using view [In debug mode pastes it in eclipse.]116 robot.keyPress(KeyEvent.VK_CONTROL);117 robot.keyPress(KeyEvent.VK_V);118 robot.keyRelease(KeyEvent.VK_V);119 robot.keyRelease(KeyEvent.VK_CONTROL);120 }121 122 robot.delay( 1000 * 3 );123 124 robot.keyPress(KeyEvent.VK_ENTER);125 robot.keyRelease(KeyEvent.VK_ENTER);126 127 robot.delay( 1000 * 7 );128 129 if ( takeScreenShot ) {130 // takeElementScreenshot(element);131 try {132 File tempScreen = captureScreenAsFile();133 134 File screen = new File( getFileName() );135 FileUtils.copyFile( tempScreen , screen);136 } catch (IOException e) {137 e.printStackTrace();138 }139 }140 141 return true;142 } catch (AWTException e) {143 e.printStackTrace();144 } catch (InterruptedException e) {145 e.printStackTrace();146 }147 return false;148 }149 150 /**151 * Sets the specified file-path to the ClipBoard.152 * <UL>Copy data to ClipBoard as.153 * <li> WIN [ Ctrl + C ] </li>154 * <li> MAC [ Command â + C ]</li> - https://superuser.com/questions/371513/how-to-tell-full-path-of-file-on-mac155 * 156 * @param filePath - the transferable object representing the clipboard content.157 */158 public void setClipboardData(String filePath) {159 StringSelection stringSelection = new StringSelection( filePath );160 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);161 }162 163 public boolean dragAndDrop(String locator1, ByType locatorType1, String locator2, ByType locatorType2) {164 try {165 166 By findBy1 = findBy(locator1, locatorType1);167 WebElement dragElement = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy1 ));168 169 By findBy2 = findBy(locator2, locatorType2);170 WebElement dropElement = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy2 ));171 172 Actions builder = new Actions(driver);173 builder.clickAndHold( dragElement )174 .moveToElement( dropElement )175 .release( dropElement )176 .build();177 178 return true;179 } catch ( Exception e ) {180 e.printStackTrace();181 }182 183 return false;184 }185 /**186 * Find the element offset position with respect to its parent element.187 * <pre>// https://stackoverflow.com/a/10954954/5081877188 * var elem = $("#sliderElementID");189 * var offset = elem.offset().left - elem.parent().offset().left;190 * console.dir( offset );191 * 192 * $('div > div.irs-bar').css({'left': '3.75%', 'width': '80%' });193 * <pre>194 * @param locator of an element.195 * @param locatorType to verify|identify an element.196 * @param xOffset horizontal move offset.197 * @param yOffset vertical move offset.198 * @return199 */200 public boolean slider( String locator, ByType locatorType, int xOffset, int yOffset ) {201 try {202 By findBy = findBy(locator, locatorType);203 WebElement sliderElement = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));204 Actions moveSlider = new Actions(driver);205 Action action6 = moveSlider.dragAndDropBy(sliderElement, xOffset, yOffset).build();206207 action6.perform();208 return true;209 } catch ( Exception e ) {210 e.printStackTrace();211 }212 return false;213 }214 215 public boolean rightClick( String locator, ByType locatorType ) {216 try {217 System.out.println("Right Click Element path : "+ locator);218 By findBy = findBy(locator, locatorType);219 WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));220 Actions rightClick = new Actions(driver);221 rightClick.moveToElement( element );222 rightClick.contextClick( element ).build().perform();223 return true;224 } catch ( Exception e ) {225 e.printStackTrace();226 }227 return false;228 }229}
...
Source:DriverCommand.java
...107 String GET_SCREEN_ORIENTATION = "getScreenOrientation";108 String SET_SCREEN_ROTATION = "setScreenRotation";109 String GET_SCREEN_ROTATION = "getScreenRotation";110 // W3C Actions APIs111 String ACTIONS = "actions";112 String CLEAR_ACTIONS_STATE = "clearActionState";113 // These belong to the Advanced user interactions - an element is114 // optional for these commands.115 String CLICK = "mouseClick";116 String DOUBLE_CLICK = "mouseDoubleClick";117 String MOUSE_DOWN = "mouseButtonDown";118 String MOUSE_UP = "mouseButtonUp";119 String MOVE_TO = "mouseMoveTo";120 // Those allow interactions with the Input Methods installed on121 // the system.122 String IME_GET_AVAILABLE_ENGINES = "imeGetAvailableEngines";123 String IME_GET_ACTIVE_ENGINE = "imeGetActiveEngine";124 String IME_IS_ACTIVATED = "imeIsActivated";125 String IME_DEACTIVATE = "imeDeactivate";126 String IME_ACTIVATE_ENGINE = "imeActivateEngine";...
Source:MobileDriver.java
1/*2 +Copyright 2014 Appium contributors3 +Copyright 2014 Software Freedom Conservancy4 +5 +Licensed under the Apache License, Version 2.0 (the "License");6 +you may not use this file except in compliance with the License.7 +You may obtain a copy of the License at8 +9 + http://www.apache.org/licenses/LICENSE-2.010 +11 +Unless required by applicable law or agreed to in writing, software12 +distributed under the License is distributed on an "AS IS" BASIS,13 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 +See the License for the specific language governing permissions and15 +limitations under the License.16 + */17package io.appium.java_client;18import org.openqa.selenium.ContextAware;19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.remote.Response;21import java.util.Map;22public interface MobileDriver extends WebDriver, ContextAware,23 PerformsTouchActions {24 public Response execute(String driverCommand, Map<String, ?> parameters);25}...
ACTIONS
Using AI Code Generation
1package org.seleniumhq.selenium.selenium_java;2import java.util.concurrent.TimeUnit;3import org.openqa.selenium.By;4import org.openqa.selenium.JavascriptExecutor;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.remote.DriverCommand;9import org.openqa.selenium.remote.RemoteWebDriver;10public class SeleniumJava {11 public static void main(String[] args) {12 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Documents\\Selenium\\chromedriver.exe");13 WebDriver driver = new ChromeDriver();14 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);15 WebElement searchBox = driver.findElement(By.name("q"));16 System.out.println("The value of the input field is: " + ((JavascriptExecutor)driver).executeScript("return arguments[0].value", searchBox));17 ((JavascriptExecutor)driver).executeScript("arguments[0].value = ''", searchBox);18 System.out.println("The value of the input field after clearing is: " + ((JavascriptExecutor)driver).executeScript("return arguments[0].value", searchBox));19 ((JavascriptExecutor)driver).executeScript("arguments[0].value = 'selenium'", searchBox);20 System.out.println("The value of the input field after sending keys is: " + ((JavascriptExecutor)driver).executeScript("return arguments[0].value", searchBox));21 }22}
ACTIONS
Using AI Code Generation
1package examples;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.remote.Command;6import org.openqa.selenium.remote.DriverCommand;7import org.openqa.selenium.remote.Response;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.WebDriverWait;10import java.util.HashMap;11import java.util.List;12import java.util.Map;13import java.util.concurrent.TimeUnit;14public class CustomActionCommands {15 public static void main(String[] args) throws InterruptedException {16 WebDriver driver = new org.openqa.selenium.chrome.ChromeDriver();17 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);18 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);19 WebDriverWait wait = new WebDriverWait(driver, 30);20 wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));21 WebElement element = driver.findElement(By.name("q"));22 element.sendKeys("selenium");23 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);24 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);25 elements.get(0).click();26 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!