How to find Window by variable title using TestStack.White framework? - ui-automation

I am using TestStack.White framework to automate opening new document in MS Word 2013.
I am opening Microsoft Word application with:
Application application = Application.Launch("winword.exe");
After that, I am trying to get the window by partial title:
Window window = application.GetWindow("Word", InitializeOption.NoCache);
But it throws an exception saying that there is no such window.
Window title is: Document1 - Word
The question is: How to get a window by partial title taking into consideration that the title is changing every time: "Document2 - Word", "Document3 - Word", etc.
Also tried *Word but looks like this func does not support wildcards
If I invoke:
List windows = application.GetWindows();
after launching an application, windows list is empty.
Thanks in advance,
Ostap

You can use EnumWindows to find all the open windows.
Within that callback you'll get a window handle which you can then us with GetWindowTextLength and GetWindowText
This will let you decide what window handle is to the window you want. From there you can use GetWindowThreadProcessId to retrieve the process ID for the word document.
And finally with that you can create a TestStack White application using the Application.Start()

It looks like opening window takes some noticeable time. GUI testing frameworks often have functions like Wait() to make sure the window is already created/visible/enabled. I'm not an expert in Teststack.White. Probably this document may help: http://teststackwhite.readthedocs.io/en/latest/AdvancedTopics/Waiting/

public static Window GetWindowBySubstring(this Application app, string titleSubString)
{
return app.GetWindows().FirstOrDefault(w => w.Title.Contains(titleSubString));
}

Related

Issue while adding/updating TOC in MS Word using OpenXML

I have a requirement to add/update TOC - Table of Contents in MS document using OpenXML. I am facing challenges to achieve the same. I am using MS Office 2016.
I have tried all the options from this post:
How to generate Table Of Contents using OpenXML SDK 2.0?
Also gone through Eric White videos.
I am trying to use UpdateField option and able to add empty TOC following the sample code from the above link.
However when I open the document I am not getting a pop-up dialog which will ask to update the TOC.
Here is the sample code:
var sdtBlock = new SdtBlock();
sdtBlock.InnerXml = GetTOC(); //TOC Xml
document.MainDocumentPart.Document.Body.AppendChild(sdtBlock);
DocumentFormat.OpenXml.Wordprocessing.SimpleField f;
f = new SimpleField();
f.Instruction = "sdtContent";
f.Dirty = true;
document.MainDocumentPart.Document.Body.Append(f);
var setting = document.MainDocumentPart.DocumentSettingsPart;
if (setting != null)
{
document.MainDocumentPart.DocumentSettingsPart.Settings.Append(new UpdateFieldsOnOpen() { Val = new DocumentFormat.OpenXml.OnOffValue(true)});
document.MainDocumentPart.DocumentSettingsPart.Settings.Save();
}
It is displaying default message, whereas I have valid entries (Headings).
Is it due to MS Office 2016? UpdateField Pop-up is not coming?
I don't want to go with below options:
Word Automation - Due to Microsoft.Office.Interop.Word
Word Automation Services - Require Sharepoint for this
Adding Macro - As it is asking to save the document every time I open it.
Also let me know if there is any better option to create/update TOC.
Your answer/comment is really very helpful.

Perl Selenium::ActionChains move_to_element not working

I am trying to click on a menu dropdown. The dropdown appears when the mouse pointer is on a menu element. The workaround can be by clicking on the menu element aslo but that sometimes is giving error due to wait time being large or small depending on the speed of site.Thus, I want to use ActionChains move_to_element for this. But it is not working no errors nothing but not working.
my $driver = Selenium::Chrome->new(binary=>"D:\\chromedriver_win32\\chromedriver.exe");
my $action_chains = Selenium::ActionChains->new(driver => $driver);
$elem = $driver->find_element(".//*[\#id='navl']/li[3]/a");
$action_chains->move_to_element($elem);
$driver->pause(5000);
$driver->find_element_by_xpath(".//*[\#id='navl']/li[3]/ul/li[1]/a")->click;
$driver->pause(50000);
$driver->shutdown_binary;
I am not sure if it is of any help - there are many questions about Selenium actions and action chains and many suggestions - I struggled with a similar problem, using Python Selenium bindings though.
First of all in the code above, it could be that there is no final perform() method called after move_to_element
Secondly - and that thing was my own problem and the source of lots of bafflement on my side - i discovered that in my case, after a single perform(), i couldn't reuse the same ActionChains object - there was no error or complaint, but something was not happening right. After I created a new ActionChains object, the subsequent new chain of actions and the final perform() worked as expected.

Get Word's window handle to use with WPF windows

I have a number of WPF dialogs in my Word Add-In. For one of them (and only one, strangely), it is sometimes not focused when opened. I believe I need to set its parent.
I know there is a way to set the owner of a WPF window to a HWND, but is there any way to get a HWND in Word 2010? I found this HWND property but it is Word 2013 and later only. Is there any other way to get Word's HWND, other than using GetForegroundWindow() which does not guarantee the handle for the window I actually want (or any other similar kludge)?
I found something helpful in Get specific window handle using Office interop. But all those answers are based on getting the handle for a window you're newly creating. I modified it somewhat to get an existing window, and stuffed it into a utility method.
doc is the current document.
using System.Windows.Interop;
using System.Diagnostics;
public void SetOwner(System.Windows.Window pd)
{
var wordProcs = Process.GetProcessesByName("winword").ToList();
// in read-only mode, this would be e.g. "1.docx [Read-Only] - Microsoft Word"
var procs = wordProcs.Where(x =>
x.MainWindowTitle.StartsWith(Path.GetFileName(doc.FullName))
&&
x.MainWindowTitle.EndsWith("- Microsoft Word"));
if (procs.Count() >= 1)
{
// would prefer Word 2013's Window.HWND property for this
var handle = procs.First().MainWindowHandle;
WindowInteropHelper wih = new WindowInteropHelper(pd);
wih.Owner = handle;
}
}
Unfortunately it doesn't seem to be possible to account for multiple windows with the same document name (in different folders), because the number of processes is never greater than 1. But I think that's an acceptable limitation.

Poltergeist-phantomjs - switching to the new popped up window

Test env: Capybara,poltergeist, phantomjs.
A new window opens up when I click a link in my test case. I was able to switch to the new window and verify text using selenium driver. However, I am unable to switch to the new window with poltergeist. I tried the following method to switch to the new window and none of them worked.
I wanted to see if a new browser is getting open at all and looks like it is.
main = page.driver.browser.window_handles.first
puts main (gives 0)
popup = page.driver.browser.window_handles.last
puts popup (gives 1)
1. within_window(->{ page.title == '2015-11-5.pdf' }) { assert_text(facility_name) }
2. page.switch_to_window(window=popup)
3. page.switch_to_window(page.driver.browser.window_handles.last)
4. page.driver.browser.switch_to().window(page.driver.browser.window_handles.last)
Could someone provide any inputs here? Thanks!
I used the following and the popup is getting generated and the control switches to it.
page.switch_to_window(page.window_opened_by{click_link('Generate Report')})
The new window has a pdf embedded in it.I was able to read and verify the contents of the document when I use selenium driver. With poltergeist, I am unable to read the pdf. Could you give me some pointers on how to proceed?
Capybara has a number of cross driver methods for dealing with this without having to go to driver specific methods.
popup = page.window_opened_by {
click_link('whatever link opens the new window')
}
within_window(popup) do
# perform actions in the new window
end

trying to access Thunderbird-tabmail does not work

I want to open a new tab with a gloda conversation from inside calendar code.
I receive an error from error console:
window not defined (or document not defined), depending on which of the two I use to Access tabmail:
let tabmail = window.document.getElementById("tabmail");
let tabmail = document.getElementById("tabmail");
The code works fine if the js file is included in an overlay xul-file.
But I want to use it outside of xul in my code.
Somewhere in my calendar code (in my 'addevent'), the same code throws the error.
This code is originally called from a rightclick on an email, but several layers deep into calendar code.
In MDN, I read that window is global? So what do I Need to do to add an tab?
This part works if tabmail is properly referenced:
tabmail.openTab("glodaList", {
collection: queryCollection,
message: aCollection.items[0],
title: tabTitle,
background: false
});
So how do I get a reference for tabmail?
Any help is appreciated.
after trying and looking through code for really some time before posting, it took only ca. 20 minutes to accidentally find the solution after submitting the question..
While browsing mailutils on mxr for something else, I found the solution in some function:
mail3PaneWindow = Services.wm.getMostRecentWindow("mail:3pane");
if (mail3PaneWindow) var tabmail = mail3PaneWindow.document.getElementById("tabmail");