Best Karate code snippet using com.intuit.karate.robot.win.IUIAutomation
Source:WinRobot.java
...45 *46 * @author pthomas347 */48public class WinRobot extends RobotBase {49 protected static final IUIAutomation UIA = IUIAutomation.INSTANCE;50 public WinRobot(ScenarioContext context, Map<String, Object> options) {51 super(context, options);52 }53 @Override54 public Map<String, Object> afterScenario() {55 logger.debug("after scenario, current window: {}", currentWindow);56 if (autoClose && command != null && currentWindow != null) {57 logger.debug("will attempt to close window for: {}", currentWindow.getName());58 WinUser.HWND hwnd = currentWindow.<IUIAutomationElement>toNative().getCurrentNativeWindowHandle();59 User32.INSTANCE.PostMessage(hwnd, WinUser.WM_QUIT, null, null);60 command.close(false);61 }62 return Collections.EMPTY_MAP;63 }64 @Override65 public List<Window> getAllWindows() {66 IUIAutomationCondition isWindow = UIA.createPropertyCondition(Property.ControlType, ControlType.Window.value);67 IUIAutomationElementArray array = UIA.getRootElement().findAll(TreeScope.Descendants, isWindow);68 int count = array.getLength();69 List<Window> list = new ArrayList(count);70 for (int i = 0; i < count; i++) {71 IUIAutomationElement e = array.getElement(i);72 if (e.isValid()) {73 list.add(new WinWindow(this, e));74 }75 }76 return list;77 }78 @Override79 protected Element windowInternal(String title) {80 return windowInternal(new StringMatcher(title));81 }82 @Override83 protected Element windowInternal(Predicate<String> condition) {84 IUIAutomationCondition isWindow = UIA.createPropertyCondition(Property.ControlType, ControlType.Window.value);85 IUIAutomationElementArray windows = UIA.getRootElement().findAll(TreeScope.Descendants, isWindow);86 int count = windows.getLength();87 for (int i = 0; i < count; i++) {88 IUIAutomationElement child = windows.getElement(i);89 if (!child.isValid()) {90 logger.warn("invalid window: {}", child);91 continue;92 }93 String name = child.getCurrentName();94 if (name == null) {95 logger.warn("name is null for window: {}", child);96 continue;97 }98 if (logger.isTraceEnabled()) {99 logger.trace("scanning window: {}", name);100 }101 if (condition.test(name)) {102 if (logger.isTraceEnabled()) {103 logger.trace("found window: {}", name);104 }105 return new WinWindow(this, child).focus();106 }107 }108 logger.warn("failed to find window: {}", condition);109 return null;110 }111 private IUIAutomationCondition by(Property property, String value) {112 return UIA.createPropertyCondition(property, value);113 }114 protected List<Element> toElements(IUIAutomationElementArray array) {115 int count = array.getLength();116 List<Element> list = new ArrayList(count);117 for (int i = 0; i < count; i++) {118 IUIAutomationElement e = array.getElement(i);119 if (e.isValid()) {120 list.add(new WinElement(this, e));121 }122 }123 return list;124 }125 @Override126 public List<Element> locateAllInternal(Element root, String locator) {127 IUIAutomationElement parent = root.<IUIAutomationElement>toNative();128 IUIAutomationCondition condition;129 if (PathSearch.isWildcard(locator)) {130 locator = "//*{" + locator + "}";131 }132 if (locator.startsWith("/")) {133 if (locator.startsWith("/root")) {134 locator = locator.substring(5);135 parent = UIA.getRootElement();136 }137 List<Element> searchResults = new ArrayList();138 PathSearch search = new PathSearch(locator, true);139 walkPathAndFind(searchResults, search, UIA.getControlViewWalker(), parent, 0);140 return searchResults;141 } else if (locator.startsWith("#")) {142 condition = by(Property.AutomationId, locator.substring(1));143 } else {144 condition = by(Property.Name, locator);145 }146 IUIAutomationElementArray found = parent.findAll(TreeScope.Descendants, condition);147 return toElements(found);148 }149 @Override150 public Element locateInternal(Element root, String locator) {151 IUIAutomationElement parent = root.<IUIAutomationElement>toNative();152 IUIAutomationCondition condition;153 if (PathSearch.isWildcard(locator)) {154 locator = "//*{" + locator + "}";155 }156 if (locator.startsWith("/")) {157 if (locator.startsWith("/root")) {158 locator = locator.substring(5);159 parent = UIA.getRootElement();160 }161 List<Element> searchResults = new ArrayList();162 PathSearch search = new PathSearch(locator, false);163 walkPathAndFind(searchResults, search, UIA.getControlViewWalker(), parent, 0);164 if (searchResults.isEmpty()) {165 return null;166 } else {167 return searchResults.get(0);168 }169 } else if (locator.startsWith("#")) {170 condition = by(Property.AutomationId, locator.substring(1));171 } else {172 condition = by(Property.Name, locator);173 }174 IUIAutomationElement found = parent.findFirst(TreeScope.Descendants, condition);175 if (!found.isValid()) { // important in this case176 return null;177 }178 return new WinElement(this, found);179 }180 @Override181 public Element getRoot() {182 return new WinElement(this, UIA.getRootElement());183 }184 @AutoDef185 @Override186 public Element getFocused() {187 return new WinElement(this, UIA.getFocusedElement());188 }189 private void walkPathAndFind(List<Element> searchResults, PathSearch search,190 IUIAutomationTreeWalker walker, IUIAutomationElement e, int depth) {191 PathSearch.Chunk chunk = search.chunks.get(depth);192 IUIAutomationCondition condition;193 ControlType controlType;194 if (chunk.controlType == null || "*".equals(chunk.controlType)) {195 condition = UIA.getControlViewCondition();196 controlType = null;197 } else {198 controlType = ControlType.fromName(chunk.controlType);199 condition = UIA.createPropertyCondition(Property.ControlType, controlType.value);200 }201 IUIAutomationElementArray array = e.findAll(chunk.anyDepth ? TreeScope.Descendants : TreeScope.Children, condition);202 if (!array.isValid()) { // the tree can be unstable203 return;204 }205 int count = array.getLength();206 boolean leaf = depth == search.chunks.size() - 1;207 for (int i = 0; i < count; i++) {208 if (chunk.index != -1 && chunk.index != i) {209 continue;210 }211 IUIAutomationElement child = array.getElement(i);212 if (!child.isValid()) { // the tree can be unstable213 continue;214 }215 if (chunk.nameCondition != null) {216 String name = child.getCurrentName();217 if (!chunk.nameCondition.test(name)) {218 continue;219 }220 }221 if (chunk.className != null) {222 String className = child.getClassName();223 if (!chunk.className.equalsIgnoreCase(className)) {224 continue;225 }...
Source:WinElement.java
...34 *35 * @author pthomas336 */37public class WinElement implements Element {38 protected final IUIAutomationElement e;39 private final WinRobot robot;40 public WinElement(WinRobot robot, IUIAutomationElement e) {41 this.robot = robot;42 this.e = e;43 }44 @Override45 public RobotBase getRobot() {46 return robot;47 }48 @Override49 public boolean isPresent() {50 return e.isValid() && !e.isNull();51 }52 @Override53 public boolean isEnabled() {54 return e.getCurrentIsEnabled();55 }56 @Override57 public boolean isImage() {58 return false;59 }60 @Override61 public Region getRegion() {62 WinDef.RECT rect = e.getCurrentBoundingRectangle();63 return new Region(robot, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);64 }65 private Location getClickablePoint() {66 WinDef.POINT p = e.getClickablePoint();67 return p == null ? getRegion().getCenter() : new Location(robot, p.x, p.y);68 }69 @Override70 public Element focus() {71 e.setFocus();72 return this;73 }74 public Element invoke() {75 if (isInvokePatternAvailable()) {76 IUIAutomationInvokePattern invokePattern = e.getCurrentPattern(IUIAutomationInvokePattern.class);77 invokePattern.invoke();78 } else {79 click();80 }81 return this;82 }83 @Override84 public Element click() {85 getClickablePoint().click();86 return this;87 }88 @Override89 public Element move() {90 getClickablePoint().move();91 return this;92 }93 @Override94 public Element press() {95 getClickablePoint().press();96 return this;97 }98 @Override99 public Element release() {100 getClickablePoint().release();101 return this;102 }103 @Override104 public String getName() {105 return e.getCurrentName();106 }107 private boolean isValuePatternAvailable() {108 Variant.VARIANT variant = e.getCurrentPropertyValue(Property.IsValuePatternAvailable);109 return variant.booleanValue();110 }111 private boolean isInvokePatternAvailable() {112 Variant.VARIANT variant = e.getCurrentPropertyValue(Property.IsInvokePatternAvailable);113 return variant.booleanValue();114 }115 @Override116 public String getValue() {117 if (isValuePatternAvailable()) {118 return e.getCurrentPattern(IUIAutomationValuePattern.class).getCurrentValue();119 }120 return null;121 }122 @Override123 public Element clear() {124 if (isValuePatternAvailable()) {125 IUIAutomationValuePattern valuePattern = e.getCurrentPattern(IUIAutomationValuePattern.class);126 valuePattern.setCurrentValue("");127 } else {128 e.setFocus();129 robot.clearFocused();130 }131 return this;132 }133 @Override134 public Element input(String value) {135 if (isValuePatternAvailable()) {136 IUIAutomationValuePattern valuePattern = e.getCurrentPattern(IUIAutomationValuePattern.class);137 valuePattern.setCurrentValue(value);138 } else {139 e.setFocus();140 robot.input(value);141 }142 return this;143 }144 @Override145 public Element delay(int millis) {146 robot.delay(millis);147 return this;148 }149 @Override150 public List<Element> getChildren() {151 IUIAutomationElementArray array = e.findAll(TreeScope.Children, WinRobot.UIA.createTrueCondition());152 int count = array.getLength();153 List<Element> list = new ArrayList(count);154 for (int i = 0; i < count; i++) {155 IUIAutomationElement child = array.getElement(i);156 list.add(new WinElement(robot, child));157 }158 return list;159 }160 private IUIAutomationTreeWalker walk() {161 return WinRobot.UIA.getControlViewWalker();162 }163 @Override164 public Element getParent() {165 return new WinElement(robot, walk().getParentElement(e));166 }167 public Element getFirstChild() {168 return new WinElement(robot, walk().getFirstChildElement(e));169 }170 public Element getLastChild() {171 return new WinElement(robot, walk().getLastChildElement(e));172 }173 public Element getNextSibling() {174 return new WinElement(robot, walk().getNextSiblingElement(e));175 }176 public Element getPreviousSibling() {177 return new WinElement(robot, walk().getPreviousSiblingElement(e));178 }179 @Override180 public IUIAutomationElement toNative() {181 return e;182 }183 @Override184 public String getDebugString() {185 if (!e.isValid()) {186 return "(null)";187 }188 try {189 return "'" + e.getCurrentName() + "' " + e.getControlType();190 } catch (Exception e) {191 return "(stale) " + e.getMessage();192 }193 }194 @Override195 public String toString() {196 return getDebugString();197 }198 @Override199 public Element select() {200 IUIAutomationSelectionItemPattern pattern = e.getCurrentPattern(IUIAutomationSelectionItemPattern.class);201 pattern.select();202 return this;203 }204 public Object as(String patternName) {205 Pattern pattern = Pattern.fromName(patternName);206 if (pattern == null) {207 throw new RuntimeException("no such pattern: " + patternName);208 }209 if (pattern.type == null) {210 throw new RuntimeException("pattern not implemented: " + pattern);211 }212 return e.getCurrentPattern(pattern.type);213 }214}...
IUIAutomation
Using AI Code Generation
1import com.intuit.karate.robot.win.IUIAutomation;2import com.intuit.karate.robot.win.IUIAutomationElement;3import com.intuit.karate.robot.win.IUIAutomationElementArray;4import com.intuit.karate.robot.win.IUIAutomationTreeWalker;5import com.intuit.karate.robot.win.IUIAutomationCondition;6import com.intuit.karate.robot.win.IUIAutomationPropertyCondition;7import com.intuit.karate.robot.win.IUIAutomationAndCondition;8import com.intuit.karate.robot.win.IUIAutomationNotCondition;9import com.intuit.karate.robot.win.IUIAutomationOrCondition;10import com.intuit.karate.robot.win.IUIAutomationCacheRequest;11import com.intuit.karate.robot.win.IUIAutomationElement;12import com.intuit.karate.robot.win.IUIAutomationElementArray;13import com.intuit.karate.robot.win.IUIAutomationTreeWalker;14import com.intuit.karate.robot.win.IUIAutomation;15import com.intuit.karate.robot.win.IUIAutomationElement;16import com.intuit.karate.robot.win.IUIAutomationElementArray;17import com.intuit.karate.robot.win.IUIAutomationTreeWalker;18import com.intuit.karate.robot.win.IUIAutomationCacheRequest;19import com.intuit.karate.robot.win.IUIAutomationElement;20import com.intuit.karate.robot.win.IUIAutomationElementArray;21import com.intuit.karate.robot.win.IUIAutomationTreeWalker;22import com.intuit.karate.robot.win.IUIAutomationCacheRequest;23import com.intuit.karate.robot.win.IUIAutomationElement;24import com.intuit.karate.robot.win.IUIAutomationElementArray;25import com.intuit.karate.robot.win.IUIAutomationTreeWalker;26import com.intuit.karate.robot.win.IUIAutomationCondition;27import com.intuit.karate.robot.win.IUIAutomationPropertyCondition;28import com.intuit.karate.robot.win.IUIAutomationAndCondition;29import com.intuit.karate.robot.win.IUIAutomationNotCondition;30import com.intuit.karate.robot.win.IUIAutomationOrCondition;31import com.intuit.karate.robot.win.IUIAutomationCacheRequest;32import com.intuit.karate.robot.win.IUIAutomationElement;33import com.intuit.karate.robot.win.IUIAutomationElementArray;34import com.intuit.karate.robot.win.IUIAutomationTreeWalker;
IUIAutomation
Using AI Code Generation
1import com.intuit.karate.robot.win.IUIAutomation;2import com.intuit.karate.robot.win.IUIAutomationElement;3import com.intuit.karate.robot.win.IUIAutomationTreeWalker;4import com.intuit.karate.robot.win.IUIAutomationCondition;5import com.intuit.karate.robot.win.IUIAutomationCacheRequest;6import com.intuit.karate.robot.win.IUIAutomationElementArray;7import com.intuit.karate.robot.win.IUIAutomationEventHandler;8import com.intuit.karate.robot.win.IUIAutomationPropertyChangedEventHandler;9import com.intuit.karate.robot.win.IUIAutomationFocusChangedEventHandler;10import com.intuit.karate.robot.win.IUIAutomationStructureChangedEventHandler;11import com.intuit.karate.robot.win.IUIAutomationTextEditTextChangedEventHandler;12import com.intuit.karate.robot.win.IUIAutomationTextSelectionChangedEventHandler;13import com.intuit.karate.robot.win.IUIAutomationTextLayoutChangedEventHandler;14import com.intuit.karate.robot.win.IUIAutomationTextAttributeChangedEventHandler;15import com.intuit.karate.robot.win.IUIAutomationTextTextChangedEventHandler;16import com.intuit.karate.robot.win.IUIAutomationTextAsyncContentLoadedEventHandler;17import com.intuit.karate.robot.win.IUIAutomationWindowOpenedEventHandler;18import com.intuit.karate.robot.win.IUIAutomationWindowClosedEventHandler;19import com.intuit.karate.robot.win.IUIAutomationMenuOpenedEventHandler;20import com.intuit.karate.robot.win.IUIAutomationMenuClosedEventHandler;21import com.intuit.karate.robot.win.IUIAutomationNotificationEventHandler;22import com.intuit.karate.robot.win.IUIAutomationPropertyChangedEventHandler;23import com.intuit.karate.robot.win.IUIAutomationStructureChangedEventHandler;24import com.intuit.karate.robot.win.IUIAutomationTextEditTextChangedEventHandler;25import com.intuit.karate.robot.win.IUIAutomationTextSelectionChangedEventHandler;26import com.intuit.karate.robot.win.IUIAutomationTextLayoutChangedEventHandler;27import com.intuit.karate.robot.win.IUIAutomationTextAttributeChangedEventHandler;28import com.intuit.karate.robot.win.IUIAutomationTextTextChangedEventHandler;29import com.intuit.karate.robot.win.IUIAutomationTextAsyncContentLoadedEventHandler;30import com.intuit.karate.robot.win.IUIAutomationWindowOpenedEventHandler;31import com.intuit.karate.robot.win.IUIAutomationWindowClosedEventHandler;32import com.intuit.kar
IUIAutomation
Using AI Code Generation
1import com.intuit.karate.robot.win.IUIAutomation;2import com.intuit.karate.robot.win.IUIAutomationElement;3import com.intuit.karate.robot.win.IUIAutomationElementArray;4import com.intuit.karate.robot.win.IUIAutomationTreeWalker;5import com.intuit.karate.robot.win.UIAutomation;6import com.sun.jna.Native;7import com.sun.jna.platform.win32.COM.COMException;8import com.sun.jna.platform.win32.COM.Unknown;9import com.sun.jna.platform.win32.Guid;10import com.sun.jna.platform.win32.OaIdl;11import com.sun.jna.platform.win32.Ole32;12import com.sun.jna.platform.win32.Variant;13import com.sun.jna.platform.win32.WinNT;14import com.sun.jna.platform.win32.WinNT.HRESULT;15import com.sun.jna.platform.win32.WinNT.HRESULT.SUCCEEDED;16import com.sun.jna.ptr.IntByReference;17import com.sun.jna.ptr.PointerByReference;18public class 4 {19 public static void main(String[] args) {20 System.out.println("Hello World");21 Ole32.INSTANCE.CoInitialize(null);22 PointerByReference pbr = new PointerByReference();23 HRESULT hr = Ole32.INSTANCE.CoCreateInstance(new Guid.CLSID("ff48dba4-60ef-4201-aa87-54103eef594e"), null, WinNT.CLSCTX_INPROC_SERVER, new Guid.IID("30cbe57d-d9d0-452a-ab13-7ac5ac4825ee"), pbr);24 if (SUCCEEDED(hr)) {25 Unknown unknown = new Unknown(pbr.getValue());26 IUIAutomation uia = (IUIAutomation) unknown.queryInterface(new Guid.IID("ff48dba4-60ef-4201-aa87-54103eef594e"));27 IUIAutomationElement root = uia.getRootElement();28 IUIAutomationElementArray children = root.findAllBuildCache(new Guid.IID("ff48dba4-60ef-4201-aa87-54103eef594e"), 100);29 int count = children.getLength();30 System.out.println("count: " + count);31 for (int i = 0; i < count; i++) {32 IUIAutomationElement child = children.getElement(i);
IUIAutomation
Using AI Code Generation
1package com.intuit.karate.robot.win;2import com.intuit.karate.robot.win.IUIAutomation;3public class 4 {4 public static void main(String[] args) {5 IUIAutomation automation = new IUIAutomation();6 automation.init();7 automation.findWindowByTitle("Calculator");8 automation.clickButton("1");9 automation.clickButton("2");10 automation.clickButton("3");11 automation.clickButton("4");12 automation.clickButton("5");13 automation.clickButton("6");14 automation.clickButton("7");15 automation.clickButton("8");16 automation.clickButton("9");17 automation.clickButton("0");18 automation.clickButton("Add");19 automation.clickButton("1");20 automation.clickButton("2");21 automation.clickButton("3");22 automation.clickButton("4");23 automation.clickButton("5");24 automation.clickButton("6");25 automation.clickButton("7");26 automation.clickButton("8");27 automation.clickButton("9");28 automation.clickButton("0");29 automation.clickButton("Equals");30 automation.quit();31 }32}33Exception in thread "main" java.lang.UnsatisfiedLinkError: com.intuit.karate.robot.win.IUIAutomation.init()V34 at com.intuit.karate.robot.win.IUIAutomation.init(Native Method)35 at com.intuit.karate.robot.win.4.main(4.java:13)
IUIAutomation
Using AI Code Generation
1package com.intuit.karate.robot.win;2import com.intuit.karate.Logger;3import com.intuit.karate.StringUtils;4import com.intuit.karate.core.ScenarioContext;5import com.intuit.karate.core.ScenarioEngine;6import com.intuit.karate.core.ScenarioExecutionUnit;7import com.intuit.karate.core.ScenarioRuntime;8import com.intuit.karate.core.ScenarioUtils;9import com.intuit.karate.core.Step;10import com.intuit.karate.core.StepResult;11import com.intuit.karate.core.StepType;12import com.intuit.karate.core.Variable;13import com.intuit.karate.core.VariableScope;14import com.intuit.karate.driver.AppiumDriver;15import com.intuit.karate.driver.DriverOptions;16import com.intuit.karate.driver.DriverOptions.DriverType;17import com.intuit.karate.driver.DriverOptions.PlatformType;18import com.intuit.karate.driver.DriverOptions.WindowType;19import com.intuit.karate.driver.DriverUtils;20import com.intuit.karate.driver.Element;21import com.intuit.karate.driver.ElementOptions;22import com.intuit.karate.driver.ElementType;23import com.intuit.karate.driver.KarateDriver;24import com.intuit.karate.driver.KarateDriverFactory;25import com.intuit.karate.driver.WebDriver;26import com.intuit.karate.driver.WebDriverWrapper;27import com.intuit.karate.driver.appium.AppiumDriverWrapper;28import com.intuit.karate.driver.appium.AppiumDriverWrapper.AppiumDriverType;29import com.intuit.karate.driver.appium.AppiumDriverWrapper.AppiumPlatformType;30import com.intuit.karate.driver.appium.AppiumDriverWrapper.AppiumWindowType;31import com.intuit.karate.driver.appium.AppiumDriverWrapper.MobileDriverType;32import com.intuit.karate.exception.KarateException;33import com.intuit.karate.http.Cookie;34import com.intuit.karate.http.HttpClient;35import com.intuit.karate.http.HttpClientFactory;36import com.intuit.karate.http.HttpRequest;37import com.intuit.karate.http.HttpResponse;38import com.intuit.karate.http.HttpUtils;39import com.intuit.karate.http.MultiPartItem;
IUIAutomation
Using AI Code Generation
1import com.intuit.karate.robot.win.IUIAutomation;2def desktop = IUIAutomation.getDesktop()3def window = desktop.findChild("My Window")4def button = window.findChild("My Button")5button.click()6import com.intuit.karate.robot.win.IUIAutomation;7def desktop = IUIAutomation.getDesktop()8def window = desktop.findChild("My Window")9def button = window.findChild("My Button")10button.click()11import com.intuit.karate.robot.win.IUIAutomation;12def desktop = IUIAutomation.getDesktop()13def window = desktop.findChild("My Window")14def button = window.findChild("My Button")15button.click()16import com.intuit.karate.robot.win.IUIAutomation;17def desktop = IUIAutomation.getDesktop()18def window = desktop.findChild("My Window")19def button = window.findChild("My Button")20button.click()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!