New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

lc2

Package Overview
Dependencies
Maintainers
2
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lc2

browser automation library

  • 1.4.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
56
increased by80.65%
Maintainers
2
Weekly downloads
 
Created
Source

Build Status Coverage Status

#lemoncase2

It's a tiny language for writing tests.

Grammer

Statements end with a semicolon

Identifier

  • Samilar with JavaScript
  • Do not need to declare
  • Avoid to use keyword

Datatype

No object, No array, LC2 is simple and just happens to be able to use.

  • number
  • string
  • boolean
  • RegExp

RegExp is a string pattern for matching if use to ~~. Else it is a random string matched the pattern.

Macro

Macro will effect the case in the whole runtime.

#TIMES [times:number|1]

// The case will try to run 10 times continuously.
#TIMES 10

#INTERVAL [interval:number|3000]

// The case will wait 1000ms and start next time after this time finished.
#INTERVAL 1000

#CLOCK [clock:number>5|10]

// The clock of case, excuting a instruction per 30ms.
#CLOCK 30

#SCREEN [width:number],[height:number]

// Set the screen width 800px height 600px when the case run.
#SCREEN 800,600

#AUTOSCROLL

// Auto scrolling the window when the trigger instruction excute in case.
#AUTOSCROLL

Instruction keyword

[action] [selector:string] by/to [params|params1,params2]

Simulate an action

assert [exp][ in [overtime:number]]

Assert that the expression is true[ within overtime].

wait [time:number]

Wait a few time like a person.

log [exp]

Record the value of an expression.

jumpto [exp]

Jump to a URL.

refresh

Refresh the page.

process [identifier] { [statements] }

Define a test process.

Action keyword

You can not dispatch any event on a <iframe> element by real devices, so actions can not use on them.

Any error will cause the Case to break and record to the log. Like no element existed, the rule of element not include the action.

input

Input a string to editable element like <input type="text" /> or <textarea>.

// <input id="link" class="case" />
input 'div#link.case' by "hello world.";

click

Click a HTML element.

// <a href="...">Text</a>
click 'a';

// <a id="link" href="...'>Text</a>
click 'a#link';

dblclick

Double click a HTML element.

// <div id="link" class="case">Text</div>
dblclick 'div#link.case';

rclick

Right click a HTML element.

// <div id="link" class="case">Text</div>
rclick 'div#link.case';

select [selector:string] by [value:string|index:number]

Select a <option> in <select> by value|index. (just use to <select>)

// <select>
//   <option value="a"></option> // index:0
//   <option value="b"></option> // index:1 <-- want to select
//   <option value="c"></option> // index:2
// </select>

select "select" by 1;

// OR

select "select" by "b";

movein - bata

Mouse move in. Like click.

moveout - bata

Mouse move out. Like click.

scroll - bata

Scroll a page or element.

Expression

  • Expression design of LC2 just looks like javascript & C
  • It is more easier and useful to descript a test.

Extended selector

  • Native querySelectorAll.
  • jQuery Sizzle ^2.3.0.
    • It means you can use some useful pseudo class like :contain().
  • Cross iframe oprator <.
    • This selector automatically filters out <iframe>
<!-- From top frame -->
<iframe id="top">
    #document
    <html>
        <body>
            <iframe id="project">
                #document
                <html>
                    <body>
                        <a href="./">link</a> <!-- select it. -->
                        <b>link</b>
                    </body>
                </html>
            </iframe>
        </body>
    </html>
</iframe>
  • iframe < iframe < a
  • * < * < a
  • #top < #project < a
  • * < #project < a:contain(link)

Selector operator

<# [selector:string] />

//Get length of the element set. It is a number (>0).
// <div id="link" class="case">Text</div>

log <# 'div'/>; // Output 1
log <# '#link'/>; // Output 1
log <# '#no'/>; // Output 0

<@ [selector:string] />

// Get the innerHTML of the 1st element in query set.
// The result is a string if element is existed.
// It is `false` if element is not existed.

// <div id="link" class="case">Text</div>

log <@ 'div'/>; // Output Text
log <@ '#link'/>; // Output Text
log <@ '#no'/>; // Output false

<! [selector:string] />

// Check the element is visibale or not.
// The result is `true` if element is existed && width > 0 && height > 0 else `false`.

// <div id="link" class="case">Text</div>
// <div id="link2" style="display:none">Text</div>

log <@ 'a'/>; // Output false
log <@ 'div'/>; // Output true
log <@ '#link2'/>; // Output false

[[property:string]:[selector:string]] - beta

Get the value of property of the element query by selector.

{[cssAttr:string]:[selector:string]} - beta

Get the value of css attribute of the element query by selector.

Compare

[exp_A:string] ~~ [exp_B:string|RegExp]

  • exp_B is not a string or RegExp, false.
  • exp_A is not a string, false.
  • exp_A is not matched to exp_B(RegExp), false.
  • exp_A has a sub string like exp_B, true.
  • exp_A is matched to exp_B(RegExp), true.

[exp_A:string] !~ [exp_B:string|RegExp]

  • !(exp_A ~~ exp_B).

Scope

  • Global scope. So there is not return keyword in LC2. A process has no paramater.
  • process main is the program entry. It must be defined.

Example

Here are some examples for inspiration.

A blank case

process main {
    log 'hello world';
}

The usual

Test case for function point just need run 1 timee.

#TIMES 1

process main {
    jumpto '[URL]';
    wait 2000;
    ...
    [statement]
    ...
}

Typical examples

A register page.

#TIMES 1
#INTERVAL 3000

process main {
	jumpto '/#/register';
	wait 2000;
	// There are 5 text boxes.
	assert <#'#userInfoFrom input'/> === 5;
	// There is a captcha svg.
	assert <!"img.pull-right"/>;
	// There is a login button.
	assert <!"#userInfoFrom > button.btn"/>;
	// There is a home button.
	assert <@"#global-back-button"/> ~~ 'glyphicon-home';

	userNameRule = "#name-rule > div";

	// Input username length < 6
	input "#account-name" by '12345';
	assert <@userNameRule/> ~~ '不少于6个字符';

    // Input username length > 16
	input "#account-name" by /\w{17}/;
	assert <@userNameRule/> ~~ '不超过16个字符';

	// Username is blank
	input "#account-name" by '';
	assert <@userNameRule/> ~~ '用户名不为空';

	// Username is repeat.
	input "#account-name" by 'active';
	click "#account-email";
	assert <!"#name-repeat"/> && <@"#name-repeat"/> ~~ '用户名重复' in 2000;
	
	// Blank email.
	emailRule = "#email-rule > div";
	input "#account-email" by '';
	assert <@emailRule/> ~~ '邮箱不为空';

	// Error email
	input "#account-email" by 'fdsf';
	assert <@emailRule/> ~~ '请输入正确格式的邮箱';

	// Repeat email
	input "#account-email" by 'lichao@or-change.cn';
	click "#account-password";
	assert <!"#emial-repeat"/> && <@"#emial-repeat"/> ~~ '邮箱重复' in 2000;
	
	// Input password length < 8
	input "#account-password" by /\w{1,7}/;
	assert <@"#password-rule > div"/> ~~ '不少于8个字符';
	// Input password length >20
	input "#account-password" by /\w{20,}/;
	assert <@"#password-rule > div"/> ~~ '不超过20个字符';
	// Blank password
	input "#account-password" by '';
	assert <@"#password-rule > div"/> ~~ '密码不为空';
	// Error char.
	input "#account-password" by '测试测试测试测试';
	assert <@"#password-rule > div"/> ~~ '您输入了非法字符';
	
	// Password !== Confirm
	password = /\w{10}/;
	input "#account-password" by password;
	input "#account-password-confirm" by password + 'a';
	assert <@"#confirm-rule > div"/> ~~ '两次输入不一致';
	
}

Keywords

FAQs

Package last updated on 11 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc