Snippet samples

Hungry Bulldozer

Moderator
Регистрация
12.01.2011
Сообщения
3 441
Благодарностей
831
Баллы
113
In this thread we will post samples of c# snippets

1 Alert with 2 buttons: "Yes" and "No":
Result is put to project's variable
1 - "Yes" was pressed
0 - "No" was pressed
-1 - "Close"
JavaScript:
// Show message
System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Press \"Yes\" or \"No\"", "Caption", System.Windows.Forms.MessageBoxButtons.YesNo);
// parse result
switch (result)
{
    // if "Yes"
    case System.Windows.Forms.DialogResult.Yes:
		// here you can put your code
		// or like this
		return 1;
		break;
    // если нет
    case System.Windows.Forms.DialogResult.No:
		// here you can put your code
		// or like this
        return 0;
        break;
    // if something else, for instance DialogResult.Cancel
    default:
		// here you can put your code
		// or like this
		return -1;
        break;
}
Link to the original post: http://zennolab.com/discussion/showthread.php?7860-Alert-C&p=45044&viewfull=1#post45044

2 Get last error:

This c# code has to be placed in bad end.

JavaScript:
var error = project.GetLastError();
var tmp = "";
if(error != null)
    tmp = string.Format("ActionComment: {0}.\r\nActionGroupId: {1}.\r\nActionId: {2}", error.ActionComment, error.ActionGroupId, error.ActionId);
 
return tmp;
3 Complete all checkboxes on the page:

On the page all tags input:checkbox of active tag are filled with value from "CheckboxValue" ({-Variable.CheckboxValue-}).
"CheckboxValue" can be True or False.
JavaScript:
// if page is not loaded yet, wait loading
if (instance.ActiveTab.IsBusy) instance.ActiveTab.WaitDownloading();
// find all checkbox on the page of active tab
HtmlElementCollection heCol = instance.ActiveTab.FindElementsByTags("input:checkbox");
// go through all checkboxes and fill them in
foreach(HtmlElement he in heCol.Elements)
{
    // fill checkbox
    he.SetValue(project.Variables["CheckboxValue"].Value, instance.EmulationLevel, false);
    // emulation delay
    instance.WaitFieldEmulationDelay();
}
4 Fill in all selects on the active tab page by last options:

It does work with classic select tags.
JavaScript:
// if page is not loaded yet, wait loading
if (instance.ActiveTab.IsBusy) instance.ActiveTab.WaitDownloading();
// find all selects on the page
HtmlElementCollection heCol = instance.ActiveTab.FindElementsByTags("select");
// go through all selects and fill them in
foreach(HtmlElement he in heCol.Elements)
{
    // Find all options of select
    HtmlElementCollection children = he.FindChildrenByTags("option");
    // if there is any element
    if (children.Count > 0)
    {
        // get last
        HtmlElement c = children.Elements[children.Count-1];
        // if element is not empty
        if (!c.IsVoid)
        {
            // get element value
            string outerText = c.GetAttribute("outertext");
            // set value
            he.SetSelectedItems(outerText);
            // emulation delay
            instance.WaitFieldEmulationDelay();
        }
    }
}
 
Последнее редактирование:

morpheus93

Client
Регистрация
25.01.2012
Сообщения
1 035
Благодарностей
237
Баллы
63
Thanks HB. This will be very helpful for all "non-coder" Zenno users.
 

Boxcutter

Client
Регистрация
08.06.2012
Сообщения
127
Благодарностей
72
Баллы
28
I would like to request a code snippet for the new project.GetLastError() function. I've tried the example code but couldn't get it working correctly.
 
  • Спасибо
Реакции: Kepperbes

shade

Client
Регистрация
19.11.2010
Сообщения
580
Благодарностей
346
Баллы
63

Вложения

  • Спасибо
Реакции: bigcajones и Boxcutter

Boxcutter

Client
Регистрация
08.06.2012
Сообщения
127
Благодарностей
72
Баллы
28
Thanks shade. I didn't know you had to use the bad end for it to work so that's why
I was having problems. Great to see my idea for this added in because it's really helpful!
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113
Here's a couple that might come in handy....

============================================
LAUNCH WINDOWS PROGRAM

System.Diagnostics.Process.Start("PATH TO .EXE HERE");

================================================

From DarkDiver:

INPUT TEXT INTO FORM WHERE CURSOR IS

{
// lock object to avoid multithreaded problems
lock(SyncObjects.InputSyncer)
{
var x = project.Variables["fname"].Value;
// activate window
Emulator.ActiveWindow("NAME OF WINDOW (IE: UNTITLED - NOTEPAD");
// send keyboard event to the active window
System.Windows.Forms.SendKeys.SendWait(x);

}
}

====================================================

System.Threading.Thread.Sleep(2000); THIS IS PAUSE


=====================================================

SCREEN CAPTURE

{int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(200, 80); (SIZE OF IMAGE YOU WANT TO SAVE)
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(450, 140, 0, 0, new Size(200, 80)); (450 AND 140 ARE X AND Y OF CORNER OF ELEMENT)
bmpScreenShot.Save(@"C:\test3.jpg");}
 
  • Спасибо
Реакции: Kepperbes

Hungry Bulldozer

Moderator
Регистрация
12.01.2011
Сообщения
3 441
Благодарностей
831
Баллы
113
Count words in a variable text:

JavaScript:
return project.Variables["text"].Value.Split( new char[] { ' ', ',', ';', '.', '!', '"', '(', ')', '?' },
    System.StringSplitOptions.RemoveEmptyEntries).Length;
 

mcyberz

Client
Регистрация
09.11.2012
Сообщения
6
Благодарностей
0
Баллы
1
hello bigcajones..
can give the exampale template for how to run it :-)


Here's a couple that might come in handy....

============================================
LAUNCH WINDOWS PROGRAM

System.Diagnostics.Process.Start("PATH TO .EXE HERE");

================================================

From DarkDiver:

INPUT TEXT INTO FORM WHERE CURSOR IS

{
// lock object to avoid multithreaded problems
lock(SyncObjects.InputSyncer)
{
var x = project.Variables["fname"].Value;
// activate window
Emulator.ActiveWindow("NAME OF WINDOW (IE: UNTITLED - NOTEPAD");
// send keyboard event to the active window
System.Windows.Forms.SendKeys.SendWait(x);

}
}

====================================================

System.Threading.Thread.Sleep(2000); THIS IS PAUSE


=====================================================

SCREEN CAPTURE

{int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(200, 80); (SIZE OF IMAGE YOU WANT TO SAVE)
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(450, 140, 0, 0, new Size(200, 80)); (450 AND 140 ARE X AND Y OF CORNER OF ELEMENT)
bmpScreenShot.Save(@"C:\test3.jpg");}
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113

Daikaiser

Client
Регистрация
06.09.2011
Сообщения
68
Благодарностей
9
Баллы
8
Hi everyone, can someone help me with a snippet to open a window where I can input some text/numbers, and the input will be saved into a variable that can be used in zennoposter? I want to manually fill some fields. Thanks!
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 769
Благодарностей
1 179
Баллы
113
Hi everyone, can someone help me with a snippet to open a window where I can input some text/numbers, and the input will be saved into a variable that can be used in zennoposter? I want to manually fill some fields. Thanks!
If you want to fill fields manually then you can use same Alert code and enter fields in browser window.

Otherwise look over there:

http://zennolab.com/discussion/threads/sozdanie-i-rabota-s-sobstvennymi-formami-oknami-windows-cherez-snippety-c.13416/
 

Daikaiser

Client
Регистрация
06.09.2011
Сообщения
68
Благодарностей
9
Баллы
8
Thanks lokiys, it works, but how can I input the data with "Enter" instead of closing the window? I am using the following snippet

System.Windows.Forms.Form F = new System.Windows.Forms.Form();
F.Text = "nameofbox";

// create a textbox
System.Windows.Forms.TextBox textb = new System.Windows.Forms.TextBox ();
// specify the location
textb.Location = new System.Drawing.Point (50,50);
// You can specify the size textbox
textb.Width = 200;
// Add it to the form
F.Controls.Add (textb);


F.ShowDialog();
return textb.Text;
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 769
Благодарностей
1 179
Баллы
113

Daikaiser

Client
Регистрация
06.09.2011
Сообщения
68
Благодарностей
9
Баллы
8
Thanks anyways lokiys, your reference helped me a lot. Is a lot better to input the text in a box than having to open each browser.
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 769
Благодарностей
1 179
Баллы
113
Thanks anyways lokiys, your reference helped me a lot. Is a lot better to input the text in a box than having to open each browser.

I re-read your post and still do not understand why you can not enter everything you need in Zennoposter browser instance ?

Use:

C#:
System.Windows.Forms.MessageBox.Show("OK");
Where you need to do manual action, and when this alert box pops up then press SHOW in zennoposter and do your manual action, then press OK in alert box and DONE...

Cheers
 
  • Спасибо
Реакции: Daikaiser

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)