I have a test(uploading a file to) which works perfectly locally(on my PC) and works perfectly on TC. But! it only works well on TC when I'm running MSTSC(Remote Desktop on my server - windows server 2012) and actually watching the test running.
When a trigger is running as part of the TeamCity build agent and running my test while I'm not on the remote desktop, then it fails. By the way, I also tried running my tests on TeamCity build agent in the background but it fails that way as well. I'm clicking an element in order to trigger the windows open dialog to pop up and then using the following:
public static void OpenFileNew(string FileNameToAttach)
{
Process pp = Process.GetCurrentProcess();
if (pp != null)
{
IntPtr h = pp.MainWindowHandle;
SetForegroundWindow(h);
}
SendKeys.SendWait("\\\\nas01\\qa\\TestFiles\\" + FileNameToAttach);
Thread.Sleep(3000);
SendKeys.SendWait("{ENTER}");
Thread.Sleep(3000);
}
Next Step is that I'm checking whether a td
Tagname has specific text.
After uploading the file , the td
should include the full file name which just been uploaded.
public bool CheckFileNameToUploadExists(string FullFileName)
{
bool IsFileExist = false;
WebDriverWait wait = new WebDriverWait(_webdriver, new TimeSpan(0, 0, 30));
var TDs = wait.Until(x => x.FindElements(By.TagName("td")));
for (int i = 0; i < TDs.Count - 1; i++)
{
var td = TDs[i].Text.ToString();
Thread.Sleep(2000);
_test.Log(LogStatus.Pass, td);
if(td == FullFileName)
IsFileExist = true;
}
return IsFileExist;
}
If it doesn't exist then an assertion is being called. that's actaully the failure.
When you leave the Remote Destktop session, you are "killing" the Destkop, and it is not possible to SetForegroundWindow
.
When you leave RDP Session, instead of logout / closing the application, you can write this little script :
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
%windir%\System32\tscon.exe %%s /dest:console
)
And run it when you want to exit the session, as an administrator.