我以前使用Selenium,现在尝试用我制作的JS文件进行自动化测试但是,无法在浏览器上打开文件。
我想运行一个js函数名。
这是源
的一部分C#
public Form1()
{
InitializeComponent();
try
{
Text = "Browser";
WindowState = FormWindowState.Normal;
ResizeBegin += (s, e) => SuspendLayout();
ResizeEnd += (s, e) => ResumeLayout(true);
Load += OnLoad;
}
private void OnLoad(object sender, EventArgs e)
{
CreateBrowser();
}
private async void CreateBrowser()
{
while (waitprocess)
{
await Task.Delay(2000);
}
waitprocess = true;
CefSettings setting = new CefSettings();
setting.UserAgent = "MOBILE USERAGENT";
try
{
setting.CachePath = "Cache/" + "1";
}
catch
{
setting.CachePath = "Cache/test";
}
Cef.Initialize(setting);
browser = new ChromiumWebBrowser("TEST WEB SITE")
{
Dock = DockStyle.Fill,
};
toolStripContainer.ContentPanel.Controls.Add(browser);
browser.FrameLoadEnd += OnBrowserFrameLoadEnd;
browser.LoadingStateChanged += OnBrowserLoadingStateChanged;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.RegisterJsObject("bound", new BoundObject());
}
我制作的JS文件通常是一个生成点击事件的函数......
例如......
function Cri_Element_Click_Tab(element, xpos)
{
var eml = element;
var nTenPercentX = parseInt(eml.offsetWidth - (eml.offsetWidth / 10));
var nTenPercentY = parseInt(eml.offsetHeight - (eml.offsetHeight / 10));
var addx = CriGenerateRandomNumber( nTenPercentX);
var addy = CriGenerateRandomNumber( nTenPercentY);
eml.focus();
var x = getOffset( eml ).left;
var y = getOffset( eml ).top;
if(x < 0)
{
var elemRect = element.getBoundingClientRect();
x= elemRect.left;
}
var mousex = window.screenX + xpos;
var mousey = window.screenY + y + addy;
//alert(mousex);
criX = parseInt(mousex);
criY = parseInt(mousey);
targetElement = eml;
fireTouchStart();
fireTouchEnd();
window.CriSendMousePointClick(criX,criY);
}