Emulator

Assembly: ZennoLab.Emulation
Full name: ZennoLab.Emulation.Emulator


Represents methods for emulation of work with windows.

In this class represented the methods which can work with windows using the system handle or header (title) of the window.

Properties

ErrorDetected

Property

Gets information about the error detected in the performance last command.

Example

// send the text
            string result = Emulator.SendText("Window", 200, 200, "it's a simple text for send");
            // if error detected
            if (Emulator.ErrorDetected) return "Fail";
            return "Text was sent";

Example2

// send the text
            $result = ZennoLab\Emulation\Emulator::SendText("Window", 200, 200, "it's a simple text for send");
            // if error detected
            if (ZennoLab\Emulation\Emulator::ErrorDetected) return "Fail";
            return "Text was sent";

Methods

SetCursorPos

Method

Установить позицию курсора

Parameters

ПараметрОписание
xКоординат X
yКоордината Y

SetFocus

Method

Установить фокус на указаном окне

Parameters

ПараметрОписание
hwndHandle окна

FindWindow

Method

Найти окно по указанным параметрам

Parameters

ПараметрОписание
lpClassNameИмя класса окна
lpWindowNameИмя окна (заголовок)

Returns: Handle окна

SetActiveWindow

Method

Установить активноее окно

Parameters

ПараметрОписание
hWndHandle окна

ShowWindow

Method

Показать окно

Parameters

ПараметрОписание
hWndHandle окна
nCmdShowСпособ отображения

SetForegroundWindow

Method

Переводит окно на передний план

Parameters

ПараметрОписание
hWndHandle окна

WindowFromPoint

Method

Получает окно по координатам

Parameters

ПараметрОписание
pКоординаты

Returns: Handle окна

GetWindowRect

Method

Получает размер указанного окна

Parameters

ПараметрОписание
hWndHandle окна
lpRectПрямоугольник окна

GetWindowText

Method

Получает текст окна

Parameters

ПараметрОписание
hwndHandle окна
sКуда записать
nMaxCountМаксимальное количество

SetWindowPos

Method

Установить полодение и размер окна

Parameters

ПараметрОписание
hWndHandle окна
hWndInsertAfter:(
XКоордината X
YКоордината Y
WДлина
HШирина
uFlagsФлаги

GetClassName

Method

Получить имя класса окна

Parameters

ПараметрОписание
hWndHandle окна
sКуда записать
nMaxCountМаксимальное количество

IsWindowExists

Method

Check for window exists.

Parameters

ПараметрОписание
name!:http://msdn.microsoft.com/ru-ru/library/system.string.aspx The header of the window.

Returns: The answer with information about the window existence. If current window exists, this answer is “true”; otherwise false.

Example

// activate the window just show it
            if (Emulator.IsWindowExists("First window"))
            {
                string result = Emulator.ActiveWindow("First window");
                // if was not any errors 
                if (result == "ok" && !Emulator.ErrorDetected)
                    return "Window activated";
            }
            return "Fail in activation window";

Example2

// activate the first window just show it
            if (ZennoLab\Emulation\Emulator::IsWindowExists("First window"))
            {
                $result = ZennoLab\Emulation\Emulator::ActiveWindow("First window");
                // if was not any errors 
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected)
                    return "Window activated";
            }
            else return "Fail: The first window";

ActiveWindow

Method

Sets the active window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter. See examples. This method calls automatically every time when you use methods from ZennoLab.Emulation.

Parameters

ПараметрОписание
name!:http://msdn.microsoft.com/ru-ru/library/system.string.aspx The header of the window.
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// activate the first window just show it
            string result = Emulator.ActiveWindow("First window");
            // if was not any errors 
            if (result == "ok" && !Emulator.ErrorDetected)
            {
                // also activate the second window just show it but set the parameters topMost == false
                result = Emulator.ActiveWindow("Second window", false);
                // if was not any errors 
                if (result == "ok" && !Emulator.ErrorDetected)
                {
                    // also activate the third window and show on top of all. Set the parameters topMost == true
                    result = Emulator.ActiveWindow("Third window", true);
                    // if all nice
                    if (result == "ok" && !Emulator.ErrorDetected) return "All in openwork boss!";
                    // if bad
                    else return "Fail: The third window";
                }
                else return "Fail: The second window";
            }
            else return "Fail: The first window";

Example2

// activate the first window just show it
            $result = ZennoLab\Emulation\Emulator::ActiveWindow("First window");
            // if was not any errors 
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected)
            {
                // also activate the second window just show it but set the parameters topMost == false
                $result = ZennoLab\Emulation\Emulator::ActiveWindow("Second window", false);
                // if was not any errors 
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected)
                {
                    // also activate the third window and show on top of all. Set the parameters topMost == true
                    $result = ZennoLab\Emulation\Emulator::ActiveWindow("Third window", true);
                    // if all nice
                    if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "All in openwork boss!";
                    // if bad
                    else return "Fail: The third window";
                }
                else return "Fail: The second window";
            }
            else return "Fail: The first window";

MaximizeWindow

Method

Performs the maximization of the specified window.

For using of this method not required the call ActivateWindow method.

Parameters

ПараметрОписание
nameThe header of the window.
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// maximize the window
            string result = Emulator.MaximizeWindow("TestForm");
            // check the result
            if (result == "ok" && !Emulator.ErrorDetected) return "Window was maximize";
            else return "Fail";

Example2

// maximize the window
            $result = ZennoLab\Emulation\Emulator::MaximizeWindow("TestForm");
            // check the result
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Window was maximize";
            else return "Fail";

MinimizeWindow

Method

Performs the minimization of the specified window.

For using of this method not required the call ActivateWindow method.

Parameters

ПараметрОписание
nameThe header of the window.
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// minimize the window
            string result = Emulator.MinimizeWindow("TestForm");
            // check the result
            if (result == "ok" && !Emulator.ErrorDetected) return "Window was minimize";
            else return "Fail";

Example2

// minimize the window
            $result = ZennoLab\Emulation\Emulator::MinimizeWindow("TestForm");
            // check the result
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Window was minimize";
            else return "Fail";

CloseWindow

Method

Closes a window with specified header.

For using of this method not required the call ActivateWindow method.

Parameters

ПараметрОписание
nameThe header of the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// close the window
            string result = Emulator.CloseWindow("TestForm");
            // check the result
            if (result == "ok" && !Emulator.ErrorDetected) return "Window was closed";
            else return "Fail";

Example2

// close the window
            $result = ZennoLab\Emulation\Emulator::CloseWindow("TestForm");
            // check the result
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Window was closed";
            else return "Fail";

SendKey

Method

Sends the keyboard events to the window with specified handle.

Parameters

ПараметрОписание
keyThe key for send.
keyboardEventThe event of key for emulation.
handleThe handle of the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// find the html element
            HtmlElement he = instance.ActiveTab.FindElementByTag("input:text", 0);
            // check the element
            if (!he.IsVoid)
            {
                // send key
                string result = Emulator.SendKey(instance.ActiveTab.Handle,System.Windows.Forms.Keys.Z, KeyboardEvent.Down); 
                // send result
                if (result == "ok" && !Emulator.ErrorDetected) return "Key was sent";
                return "Fail";
            }
            return "Element not found";

Example2

// find the html element
            $he = $instance->ActiveTab->FindElementByTag("input:text", 0);
            // check the element
            if (!$he->IsVoid)
            {
                // send key
                $result = ZennoLab\Emulation\Emulator::SendKey($instance->ActiveTab->Handle, System\Windows\Forms\Keys::Z, KeyboardEvent::Down); 
                // send result
                if ($result == "ok" && !ZennoLab\Emulation\Emulator.ErrorDetected) return "Key was sent";
                return "Fail";
            }
            return "Element not found";

SendKey

Method

Sends the keyboard events to the specified window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter. The x and y can be -1. In this case the key will be sent to the current window.

Parameters

ПараметрОписание
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.
windowNameThe header of the window.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.
keyboardEventThe event of key for emulation.
keyThe key for send.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// send the key "a"
            string result = Emulator.SendKey("Window", 200, 200, System.Windows.Forms.Keys.A, KeyboardEvent.Press);
            // check result
            if (result == "ok" && !Emulator.ErrorDetected) return "Key was sent";
            else return "Fail";

Example2

// send the key "a"
            $result = ZennoLab\Emulation\Emulator::SendKey("Window", 200, 200, System\Windows\Forms\Keys.A, KeyboardEvent::Press);
            // check result
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Key was sent";
            else return "Fail";

SendText

Method

Sends a text to the window with specified handle.

Parameters

ПараметрОписание
textThe text for send.
handleThe handle of the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// find the html element
            HtmlElement he = instance.ActiveTab.FindElementByTag("input:text", 0);
            // check the element
            if (!he.IsVoid)
            {
                // focus on this element
                he.Focus();
                // send text
                string result = Emulator.SendText(instance.ActiveTab.Handle, "Simple text"); 
                // send result
                if (result == "ok" && !Emulator.ErrorDetected) return "Key was sent";
                return "Fail";
            }
            return "Element not found";

Example2

// find the html element
            $he = $instance->ActiveTab->FindElementByTag("input:text", 0);
            // check the element
            if (!$he->IsVoid)
            {
                // focus on this element
                $he->Focus();
                // send text
                $result = ZennoLab\Emulation\Emulator::SendText($instance->ActiveTab->Handle, "Simple text"); 
                // send result
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Key was sent";
                return "Fail";
            }
            return "Element not found";

SendText

Method

Sends a text to the specified window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter. The x and y can be -1. In this case the text will be sent to the current window.

Parameters

ПараметрОписание
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.
windowNameThe header of the window.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.
textThe text for send.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// send the text
            string result = Emulator.SendText("Window", 200, 200, "it's a simple text for send");
            // check result
            if (result == "ok" && !Emulator.ErrorDetected) return "Text was sent";
            else return "Fail";

Example2

// send the text
            $result = ZennoLab\Emulation\Emulator::SendText("Window", 200, 200, "it's a simple text for send");
            // check result
            if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "Text was sent";
            else return "Fail";

MouseClick

Method

Emulates the mouse’s events in the specified window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter. The x and y can be -1. In this case the click will be performed to the current window.

Parameters

ПараметрОписание
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.
windowNameThe header of the window.
buttonEventThe event of mouse button for emulation.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.
buttonThe mouse button for emulation.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// show the save file dialog
            string result = Emulator.MouseClick("Simple window", MouseButton.Left, MouseButtonEvent.Click, 200, 200);
            // wait a little bit
            System.Threading.Thread.Sleep(2000);
            // if mouse click was successful
            if (result == "ok" && !Emulator.ErrorDetected)
            {
                // click on button with caption "Save"
                result = Emulator.ButtonClick("Save as", "Save");
                // make answer
                if (result == "ok" && !Emulator.ErrorDetected) return "All done";
                else return "Fail";
            }

Example2

// show the save file dialog
            $result = ZennoLab\Emulation\Emulator::MouseClick("Simple window", MouseButton::Left, MouseButtonEvent::Click, 200, 200);
            // wait a little bit
            System\Threading\Thread::Sleep(2000);
            // if mouse click was successful
            if ($result == "ok" && !Emulator::ErrorDetected)
            {
                // click on button with caption "Save"
                $result = ZennoLab\Emulation\Emulator::ButtonClick("Save as", "Save");
                // make answer
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "All done";
                else return "Fail";
            }

MouseClick

Method

Emulates the mouse’s events in the window with specified handle.

Parameters

ПараметрОписание
buttonThe mouse button for emulation.
buttonEventThe event of mouse button for emulation.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.
handleThe handle of the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// find the html element
            HtmlElement he = instance.ActiveTab.FindElementByTag("input:submit", 0);
            // check the element
            if (!he.IsVoid)
            {
                // click
                string result = Emulator.MouseClick(instance.ActiveTab.Handle, 
                    MouseButton.Left, MouseButtonEvent.Click, he.DisplacementInTabWindow.X + 10, he.DisplacementInTabWindow.Y + 10);
                // click was performed
                if (result == "ok") return "All done";
                // fail
                return "Fail";
            }
            // element not found
            return "Element not found";

Example2

// find the html element
            $he = $instance->ActiveTab->FindElementByTag("input:submit", 0);
            // check the element
            if (!$he->IsVoid)
            {
                // click
                $result = ZennoLab\Emulation\Emulator::MouseClick($instance->ActiveTab->Handle, 
                    ZennoLab\Emulation\MouseButton::Left, MouseButtonEvent::Click, $he->DisplacementInTabWindow->X + 10, $he->DisplacementInTabWindow->Y + 10);
                // click was performed
                if ($result == "ok") return "All done";
                // fail
                return "Fail";
            }
            // element not found
            return "Element not found";

MouseMove

Method

Moves the mouse to a specified location in the specified window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter.

Parameters

ПараметрОписание
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.
windowNameThe header of the window.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// random
            Random rnd = new Random();
            // x location
            int x = 200;
            // y location
            int y = 200;
            // offset X
            int offsetX = rnd.Next(100);
            // offset y
            int offsetY = rnd.Next(100);
            // move mouse to start location x = 200 and y = 200 on "Window" window 
            string result = Emulator.MouseMove("Window", x, y);
            // check result
            if (result != "ok" && Emulator.ErrorDetected) return "Fail";
            // mouse move to a new location
            result = Emulator.MouseMove("Window", x + offsetX, y + offsetY);
            // check result
            if (result != "ok" && Emulator.ErrorDetected) return "Fail";
            // answer
            return String.Format("Mouse was moved from x = {0}; y = {1} to x = {2}; y = {3}", x, y, x + offsetX, y + offsetY);

Example2

// random
            $rnd = new Random();
            // x location
            $x = 200;
            // y location
            $y = 200;
            // offset X
            $offsetX = $rnd->Next(100);
            // offset y
            $offsetY = $rnd->Next(100);
            // move mouse to start location x = 200 and y = 200 on "Window" window
            $result = ZennoLab\Emulation\Emulator::MouseMove("Window", $x, $y);
            // check result
            if ($result != "ok" && ZennoLab\Emulation\Emulator::ErrorDetected) return "Fail";
            // mouse move to a new location
            $result = ZennoLab\Emulation\Emulator::MouseMove("Window", $x + $offsetX, $y + $offsetY);
            // check result
            if ($result != "ok" && ZennoLab\Emulation\Emulator::ErrorDetected) return "Fail";
            // answer
            return String::Format("Mouse was moved from x = {0}; y = {1} to x = {2}; y = {3}", $x, $y, $x + $offsetX, $y + $offsetY);

MouseMove

Method

Moves the mouse to a specified location in the window with specified handle.

Parameters

ПараметрОписание
handleThe handle of the window.
xThe x coordinate relative to the window.
yThe y coordinate relative to the window.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// find the html element
            HtmlElement he = instance.ActiveTab.FindElementByTag("input:submit", 0);
            // check the element
            if (!he.IsVoid)
            {
                // move the mouse to the html element location
                string result = Emulator.MouseMove(instance.ActiveTab.Handle, he.DisplacementInTabWindow.X + 10, he.DisplacementInTabWindow.Y + 10);
                if (result == "ok" && !Emulator.ErrorDetected)
                {    
                    // wait a little bit
                    System.Threading.Thread.Sleep(200);
                    // perform the click
                    result = Emulator.MouseClick(instance.ActiveTab.Handle, MouseButton.Left, MouseButtonEvent.Click, he.DisplacementInTabWindow.X + 10, he.DisplacementInTabWindow.Y + 10);
                    if (result == "ok" && !Emulator.ErrorDetected) return "All done";
                    return "Mouse click failed";
                }
                else return "Mouse move failed";
            }
            return "Element not found";

Example2

// find the html element
            $he = $instance->ActiveTab->FindElementByTag("input:submit", 0);
            // check the element
            if (!$he->IsVoid)
            {
                // move the mouse to the html element location
                $result = ZennoLab\Emulation\Emulator::MouseMove($instance->ActiveTab->Handle, $he->DisplacementInTabWindow->X + 10, $he->DisplacementInTabWindow->Y + 10);
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected)
                {    
                    // wait a little bit
                    System\Threading.\Thread\Sleep(200);
                    // perform the click
                    $result = ZennoLab\Emulation\Emulator::MouseClick($instance->ActiveTab->Handle, MouseButton::Left, MouseButtonEvent::Click, $he->DisplacementInTabWindow->X + 10, he->DisplacementInTabWindow->Y + 10);
                    if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "All done";
                    return "Mouse click failed";
                }
                else return "Mouse move failed";
            }
            return "Element not found";

ButtonClick

Method

Clicks a button with specified caption in specified window.

There are several ways of calling this method. The parameter topMost have the default value and you can call the method without this parameter. This method searches the button by the caption and does not case sensitive.

Parameters

ПараметрОписание
topMosttrue if need to show the window on top of all; otherwise, false. Default value is false.
windowNameThe header of the window.
buttonNameThe text displayed on the button.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// show the save file dialog
            string result = Emulator.MouseClick("Simple window", MouseButton.Left, MouseButtonEvent.Click, 200, 200);
            // wait a little bit
            System.Threading.Thread.Sleep(2000);
            // if mouse click was successful
            if (result == "ok" && !Emulator.ErrorDetected)
            {
                // click on button with caption "Save"
                result = Emulator.ButtonClick("Save as", "Save");
                // make answer
                if (result == "ok" && !Emulator.ErrorDetected) return "All done";
                else return "Fail";
            }

Example2

// show the save file dialog
            $result = ZennoLab\Emulation\Emulator::MouseClick("Simple window", MouseButton::Left, MouseButtonEvent::Click, 200, 200);
            // wait a little bit
            System\Threading\Thread::Sleep(2000);
            // if mouse click was successful
            if ($result == "ok" && !Emulator::ErrorDetected)
            {
                // click on button with caption "Save"
                $result = ZennoLab\Emulation\Emulator::ButtonClick("Save as", "Save");
                // make answer
                if ($result == "ok" && !ZennoLab\Emulation\Emulator::ErrorDetected) return "All done";
                else return "Fail";
            }

DragAndDrop

Method

Performs the drag and drop events inside specified window by handle.

Parameters

ПараметрОписание
handleThe handle of the window.
fromXThe x coordinate in the window for drag event.
formYThe y coordinate in the window for drag event.
toXThe x coordinate in the window for drop event.
toYThe y coordinate in the window for drop event.

Returns: The answer with information about the success of the current command’s execution. If current command was successful then this answer is “ok”; otherwise message describing the error.

Example

// go to page with drag and drop element
            if (instance.ActiveTab.IsNull || instance.ActiveTab.IsVoid) return "Fail";
            instance.ActiveTab.Navigate("dragdroppage.com");
            // wait downloading
            if (instance.ActiveTab.IsBusy) instance.ActiveTab.WaitDownloading();
             
            // do drag and drop event
            return Emulator.DragAndDrop(instance.ActiveTab.Handle, 100, 100, 200);

Example2

// go to page with drag and drop element
            if ($instance->ActiveTab->IsNull || $instance->ActiveTab->IsVoid) return "Fail";
            $instance->ActiveTab->Navigate("dragdroppage.com");
            // wait downloading
            if ($instance->ActiveTab->IsBusy) $instance->ActiveTab->WaitDownloading();
             
            // do drag and drop event
            return ZennoLab\Emulation\Emulator::DragAndDrop($instance->ActiveTab->Handle, 100, 100, 200);