Emulator
Assembly: ZennoLab.Emulation
Full name: ZennoLab.Emulation.Emulator
Kind: class
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.
Constructor
Emulator()
Constructor
Methods
IsWindowExists
Method
bool IsWindowExists(string name)
Check for window exists.
Parameters
| Type | Name | Description |
|---|---|---|
string | 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
string ActiveWindow(string name, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | name | !:http://msdn.microsoft.com/ru-ru/library/system.string.aspx The header of the window. |
bool | topMost | true 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
string MaximizeWindow(string name, bool topMost)
Performs the maximization of the specified window.
For using of this method not required the call ActivateWindow method.
Parameters
| Type | Name | Description |
|---|---|---|
string | name | The header of the window. |
bool | topMost | true 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
string MinimizeWindow(string name, bool topMost)
Performs the minimization of the specified window.
For using of this method not required the call ActivateWindow method.
Parameters
| Type | Name | Description |
|---|---|---|
string | name | The header of the window. |
bool | topMost | true 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
string CloseWindow(string name)
Closes a window with specified header.
For using of this method not required the call ActivateWindow method.
Parameters
| Type | Name | Description |
|---|---|---|
string | name | The 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
string SendKey(int handle, Keys key, KeyboardEvent keyboardEvent)
Sends the keyboard events to the window with specified handle.
Parameters
| Type | Name | Description |
|---|---|---|
int | handle | The handle of the window. |
Keys | key | The key for send. |
KeyboardEvent | keyboardEvent | The event of key 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
// 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
string SendKey(string windowName, int x, int y, Keys key, KeyboardEvent keyboardEvent, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | windowName | The header of the window. |
int | x | The x coordinate relative to the window. |
int | y | The y coordinate relative to the window. |
Keys | key | The key for send. |
KeyboardEvent | keyboardEvent | The event of key for emulation. |
bool | topMost | true 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
// 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
string SendText(int handle, string text)
Sends a text to the window with specified handle.
Parameters
| Type | Name | Description |
|---|---|---|
int | handle | The handle of the window. |
string | text | The 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
// 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
string SendText(string windowName, int x, int y, string text, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | windowName | The header of the window. |
int | x | The x coordinate relative to the window. |
int | y | The y coordinate relative to the window. |
string | text | The text for send. |
bool | topMost | true 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
// 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
string MouseClick(string windowName, MouseButton button, MouseButtonEvent buttonEvent, int x, int y, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | windowName | The header of the window. |
MouseButton | button | The mouse button for emulation. |
MouseButtonEvent | buttonEvent | The event of mouse button for emulation. |
int | x | The x coordinate relative to the window. |
int | y | The y coordinate relative to the window. |
bool | topMost | true 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
// 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
string MouseClick(int handle, MouseButton button, MouseButtonEvent buttonEvent, int x, int y)
Emulates the mouse’s events in the window with specified handle.
Parameters
| Type | Name | Description |
|---|---|---|
int | handle | The handle of the window. |
MouseButton | button | The mouse button for emulation. |
MouseButtonEvent | buttonEvent | The event of mouse button for emulation. |
int | x | The x coordinate relative to the window. |
int | y | The 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)
{
// 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
string MouseMove(string windowName, int x, int y, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | windowName | The header of the window. |
int | x | The x coordinate relative to the window. |
int | y | The y coordinate relative to the window. |
bool | topMost | true 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
// 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
string MouseMove(int handle, int x, int y)
Moves the mouse to a specified location in the window with specified handle.
Parameters
| Type | Name | Description |
|---|---|---|
int | handle | The handle of the window. |
int | x | The x coordinate relative to the window. |
int | y | The 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
string ButtonClick(string windowName, string buttonName, bool topMost)
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
| Type | Name | Description |
|---|---|---|
string | windowName | The header of the window. |
string | buttonName | The text displayed on the button. |
bool | topMost | true 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
// 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
string DragAndDrop(int handle, int fromX, int formY, int toX, int toY)
Performs the drag and drop events inside specified window by handle.
Parameters
| Type | Name | Description |
|---|---|---|
int | handle | The handle of the window. |
int | fromX | The x coordinate in the window for drag event. |
int | formY | The y coordinate in the window for drag event. |
int | toX | The x coordinate in the window for drop event. |
int | toY | The 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);