use .net ui automation framework to capture user clicks of links in outlook application - microsoft-ui-automation

I'm using .net ui automation framework to capture user clicks of links when they are viewing a message in an outlook application.
The problem is that, I'm not able to get the link as an AutomationElement(as I can in a web page in IE window). I can only get the document pane as a whole.
Is there any way to do it?

Well, I got it myself.
Though I'm not able to get the link directly, I can locate the link and get it from the TextPattern of the document element.
Suppose element is the document element that directly gets the focus or clicked:
if (element.Current.LocalizedControlType == "document")
{
var point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
object textPattern;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out textPattern))
{
var range = ((TextPattern)textPattern).RangeFromPoint(point); //it's an empty range
var e = range.GetEnclosingElement(); //get the enclosing AutomationElement
if (e.Current.LocalizedControlType == "link" || e.Current.LocalizedControlType == "hyperlink")
{
//use e
}
}
}

Related

Using a Chrome extension to insert characters into sites such as Facebook

I have created a chrome extension to allow users to right-click in a textbox, and insert special characters. This works on many sites such as StackOverflow, but does not work on sites such as Facebook. This is because Facebook is not using a standard text box form control. Instead for each line in a text message, it seems to be using a div > div > span > span construct. Is there a way to create a Chrome extension to target page components such as this?
An portion of my Chrome extension code looks like this:
main.js:
chrome.contextMenus.create({
title: "\u038F",
contexts:["editable"],
onclick: function(info, tab){
chrome.tabs.sendMessage(tab.id, {action: "insertCharacter", character: '\u038F'});
}
});
content.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
var objField = document.activeElement;
if (request.action == "insertCharacter"){
insertAtCursor(objField, request.character);
}
});
function insertAtCursor(sField, sValue){
if (sField.selectionStart || sField.selectionStart == '0'){
var nStart = sField.selectionStart;
var nEnd = sField.selectionEnd;
sField.value = sField.value.substring(0, nStart) + sValue + sField.value.substring(nEnd, sField.value.length);
sField.selectionStart = nStart + sValue.length;
sField.selectionEnd = nStart + sValue.length;
}
else {
sField.value += sValue;
}
}
Is there a more general purpose way I can do this to handle various situations on different sites? If not, is there a way to specifically target Facebook as most of the time myself (and likely others) are going to be using my extension on Facebook. (Of course having it work for email sites such as GMail would be a benefit as well).
In case it helps someone else, this is what I modified my code to based on wOxxOm's suggestion:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
if (request.action == "insertCharacter"){
insertAtCursor(request.character);
}
});
function insertAtCursor(sValue){
document.execCommand("insertText", false, sValue);
}
It's much more compact than my original approach and insertText handles the selection aspect automatically.

Second browser instance in protractor can not access class' elements

I am using the protractor framework. I'd like to write a test checking if User 1 successfully sends message to User 2. Both users should be logged in at 2 different browsers. So, what i want to do is:
it("Test", () => {
let browser2 = browser.forkNewDriverInstance(true);
browser2.Chat.icon.click();
This way i want to click the element icon in the class Chat, which looks like:
export class Chat{
public static icon: p.ElementFinder = element(by.css("#popup > div > div > div > section > header > a"));
}
When i try do do that, the following error appear: Property Chat does not exist on type Protractor
How can i access the elements in the classes from browser2?
So, browser2 in your example is a completely new instance of the browser. The elements on the Chat property are still attached to the initial browser instance (browser). What worked for me is creating a module for switching browser (and element, by, etc.) contexts. The second answer here helped me greatly in creating that library: Multiple browsers and the Page Object pattern
I'm using the page object pattern, so what I had to do was re-instantiate new page objects after I forked the new driver instance. So it ended up something like this (Javascript).
var Loginpage = require('../loginPage.js);
var loginPage = new LoginPage();
it('user1 logs in, sends message to user2' () => {
loginPage.login()
//send your message
});
it('user2 logs in and looks for message' () => {
browser.forkNewDriverInstance(true);
var newBrowserLoginPage = new LoginPage();
newBrowserLoginPage.login()
var newBrowserNotificationsPage = new UserNotificationsPage();
newBrowserNotificationsPage.checkMessages();
});
So, I suggest building the small library for switching browser contexts (and therefore element, by, protractor, etc. contexts).

Check if the user has clicked inside a specific div in a component

I have got a global click event in my application.
host: {
'(document:click)': 'handleClick($event)',
},
Everytime the user clicks, my handleClick function will execute. I want to check if the user has clicked in a specific div. I've tried with the following:
handleClick(event){
console.log(event.target===document.getElementsByClassName("drop_down_wrapper")
}
But this does not work. I've also tried to get hold of a specific div by using ElementRef but I only managed to get hold of the native element of my div. Any suggestions?
First of all, getElementsByClassName returns a HtmlCollection, so you might want to use index to access individual items:
handleClick(event) {
console.log(event.target === document.getElementsByClassName("drop_down_wrapper")[0]
}
Or better use querySelector method:
handleClick(event) {
console.log(event.target === document.querySelector(".drop_down_wrapper")
}
However, if your div have inner HTML you would probably need to check if the clicked target is a child of the div or not:
var target = e.target;
var wrapper = document.querySelector(".drop_down_wrapper");
while (target != wrapper && target !== document) {
target = target.parentNode;
}
Add a click handler to the particular div you want to listen on, and add
event.stopPropogation();
This should prevent your document click handler from firing.

How to have an Apps Script Gadget display results directly in the UI

On my Google Site I've inserted an Apps Script Gadget (by pasting the URL of an Apps Script that I published as a service). This Apps Script allows the user to enter a value (their 'Blow Number') and view the corresponding data (based on API calls to my Google Fusion tables).
Right now, the script returns 3 hyperlinks:
Click here for a table of Blow Number 1
Click here for a chart of Blow Number 1
Click here for a map of Blow Number 1
This is because my script function getblowdetails has 3 app.createAnchor variables. Instead of having the script return 3 hyperlinks (that the user has to click on and view the resulting URL in a new window), I would like for the script to automatically invoke the 3 URLs and display the table, chart, and map in panels on the same page.
So the user would enter their Blow Number and press enter. They would then view the table, chart, and map directly below the text box on the same web page.
Please see the Code that I've included below and advise...Thanks for the help- I'm an apps-script novice so a thorough and understand-able response is greatly appreciated!
Code
Note: I've removed the URLs from the createAnchor variables because I'm only allowed to include 2 links in the post, but you can see them by going to www.OnSiteBAC.com/ViewMyBlows and entering Blow Number = 1...then click on the hyperlinks.
function doGet() {
var app = UiApp.createApplication();
// Create input boxes, buttons, labels, and links
var textBoxA = app.createTextBox().setId('textBoxA').setName('textBoxA').setFocus(true);
var buttonA = app.createButton('Get Blow Details').setEnabled(false);
var label = app.createLabel('Please enter your Blow Number here');
var link = app.createAnchor('where can I find my Blow Number?', 'http://www.onsitebac.com');
// Create a handler to call the getblowdetails function.
// A validation is added to this handler so that it will only invoke 'getblowdetails' if textBoxA contains a number
var handler = app.createServerClickHandler('getblowdetails').validateNumber(textBoxA).addCallbackElement(textBoxA);
// Create a handler to enable the button if all input is legal
var onValidInput = app.createClientHandler().validateNumber(textBoxA).forTargets(buttonA).setEnabled(true).forTargets(label, link).setVisible(false);
// Create a handler to mark invalid input in textBoxA and disable the button
var onInvalidInput1 = app.createClientHandler().validateNotNumber(textBoxA).forTargets(buttonA).setEnabled(false).forTargets(textBoxA).setStyleAttribute("color", "red").forTargets(label, link).setVisible(true);
// Create a handler to mark the input in textBoxA as valid
var onValidInput1 = app.createClientHandler().validateNumber(textBoxA).forTargets(textBoxA).setStyleAttribute("color", "black");
// only fire ServerHandler for onKeyUp if it passes validation
var textBoxHandler = app.createServerHandler('textBoxHandlerFunction');
// Add all the handlers to be called when the user types in the text boxes
textBoxHandler.addCallbackElement(textBoxA);
textBoxA.addKeyUpHandler(onInvalidInput1);
textBoxA.addKeyUpHandler(onValidInput1);
textBoxA.addKeyUpHandler(onValidInput);
textBoxA.addKeyUpHandler(textBoxHandler);
buttonA.addClickHandler(handler);
app.add(textBoxA);
app.add(buttonA);
app.add(label);
app.add(link);
return app;
}
function textBoxHandlerFunction(e) {
var app = UiApp.getActiveApplication();
if (e.parameter.keyCode == 13)
{
app = getblowdetails(e);
}
return app;
}
function getblowdetails(e) {
var app = UiApp.getActiveApplication();
var panel2 = app.createVerticalPanel();
var link2 = app.createAnchor ().setStyleAttribute("color", "green");
var panel3 = app.createVerticalPanel();
var link3 = app.createAnchor ();
var panel4 = app.createVerticalPanel();
var link4 = app.createAnchor ();
panel3.add(link3);
app.add(panel3);
panel4.add(link4);
app.add(panel4);
return app;
}
I don't think you'll be able to actually download the result and show it. So, there's no easy solution.
But you can build that table and chart on Apps Script easily (assuming you can already fetch the info from tables using its API).
The last issue is the map. On Apps Script you can only create static maps, meaning, an image. You can add custom markers and polygons, set the zoom, etc. But in the end it's a photo. The user will not be able to drag it around or use the map as an embedded google map as one would expect.

InDesign CS5 Script: How can I close all modal dialog windows in a document?

When a document does not have any modal dialog window messages, app.activeDocument.close(SaveOptions.no) works fine.
However, I have some InDesign documents that do have such windows appear, showing error messages about links that need to be updated, or incorrect styles. The above statement won't work in this case, as the windows prevent the script from accessing the document.
So, is there a way to iterate through all of the modal-dialogs in the active document? Here is what I have tried so far, which is not working:
if(xmlFile == "")
{
//alert("There is no linked XML file in this document,\n\ttry a different document.");
for(var i = 0; i < app.activeDocument.Windows.length; i++)
{
app.activeDocument.Windows[i].close();
}
app.activeDocument.close(SaveOptions.no);
exit();
}
Ok, so the "user interaction level" of the application needs to be changed to "NEVER_INTERACT" to ignore all modal dialog windows. Here is the modified code, which is now working:
if(xmlFile == "")
{
alert("There is no linked XML file in this document,\n\ttry a different document.");
// the original interaction and warning settings of the application
var oldInteractionPrefs = app.scriptPreferences.userInteractionLevel;
// prevent interaction and warnings from interrupting script
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
// close the active document without saving
app.activeDocument.close(SaveOptions.no);
// reset interactions to original settings
app.scriptPreferences.userInteractionLevel = oldInteractionPrefs;
exit();
}
Did you try it ?
app.activeDocument.windows.everyItem.close(SaveOptions.no);