GithubHelp home page GithubHelp logo

njasm / marionette_client Goto Github PK

View Code? Open in Web Editor NEW
48.0 6.0 13.0 249 KB

Mozilla's Gecko Marionette client in golang

License: MIT License

Go 100.00%
firefox webdriver mozilla-firefox marionette marionette-client golang selenium-webdriver selenium

marionette_client's Introduction

Go Reference Build Status Coverage Status Go Report Card License

marionette_client

Mozilla's Gecko Marionette client in golang

What is Marionette

"Marionette is an automation driver for Mozilla's Gecko engine. It can remotely control either the UI, or the internal JavaScript of a Gecko platform, such as Firefox. It can control both the chrome (i.e. menus and functions) or the content (the webpage loaded inside the browsing context), giving a high level of control and ability to replicate user actions. In addition to performing actions on the browser, Marionette can also read the properties and attributes of the DOM.

If this sounds similar to Selenium/WebDriver then you're correct! Marionette shares much of the same ethos and API as Selenium/WebDriver, with additional commands to interact with Gecko's chrome interface. Its goal is to replicate what Selenium does for web content: to enable the tester to have the ability to send commands to remotely control a user agent."

Resources

https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette

https://w3c.github.io/webdriver/webdriver-spec.html

Examples

Incomplete list. Check the tests for more examples.

Instantiate the client

client := NewClient()
// this are the default marionette values for hostname, and port 
client.Connect("", 0)
// let marionette generate the Session ID with it's default Capabilities
client.NewSession("", nil) 
	

Navigate to page

client.Navigate("http://www.google.com/")

Change Contexts

client.SetContext(Context(CHROME))
// or
client.SetContext(Context(CONTENT))
	

Find Element

element, err := client.FindElement(By(ID), "html-element-id-attribute")
if err != nil {
	// handle your errors
}

// else
println(element.Id())
println(element.Enabled())
println(element.Selected())
println(element.Displayed())
println(element.TagName())
println(element.Text())
println(element.Attribute("id"))
println(element.Property("id"))
println(element.CssValue("text-decoration"))
	
// width, height, x and y
rect, err := element.Rect()
if err != nil {
    // handle your errors
}

fmt.Printf("%#v", rect)
	
// size
w, h, err := element.Size()
if err != nil {
	// handle your errors
}

fmt.Printf("width: %f, height: %f", w, h)

//location
x, y, err := element.Location()
if err != nil {
    // handle your errors
}

fmt.Printf("x: %v, y: %v", x, y)

Find Elements

collection, err := element.FindElements(By(TAG_NAME), "li")
if err != nil {
	// handle your errors
}

// else
for var e := range collection {
	println(e.Id())
   	println(e.Enabled())
   	println(e.Selected())
   	println(e.Displayed())
   	println(e.TagName())
   	println(e.Text())
   	println(e.Attribute("id"))
   	println(e.CssValue("text-decoration"))
   	e.Click()
}

Execute JS Script

script := "function mySum(a, b) { return a + b; }; return mySum(arguments[0], arguments[1]);"
args := []int{1, 3} // arguments to be passed to the function
timeout := 1000     // milliseconds
sandbox := false    // new Sandbox
r, err := client.ExecuteScript(script, args, timeout, sandbox)
if err == nil {
    println(r.Value) // 4 
}

Wait(), Until() Expected condition is true.

client.Navigate("http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_get")

timeout := time.Duration(10) * time.Second
condition := ElementIsPresent(By(ID), "stackH")
ok, webElement, err := Wait(client).For(timeout).Until(condition)

if !ok {
	log.Printf("%#v", err)
	// do your error stuff
	return
}

// cool, we've the element, let's click on it!
webElement.Click()

marionette_client's People

Contributors

bors[bot] avatar dependabot-preview[bot] avatar markuszm avatar njasm avatar serv-inc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

marionette_client's Issues

Tracking: Missing WebDriver/Marionette commands

Tracking issue for missing Webdriver marionette commands:

Latest version 100.0 commands:

// Marionette service

  1. "Marionette:AcceptConnections": GeckoDriver.prototype.acceptConnections,
  2. "Marionette:GetContext": GeckoDriver.prototype.getContext,
  3. "Marionette:GetScreenOrientation": GeckoDriver.prototype.getScreenOrientation,
  4. "Marionette:GetWindowType": GeckoDriver.prototype.getWindowType,
  5. "Marionette:Quit": GeckoDriver.prototype.quit,
  6. "Marionette:SetContext": GeckoDriver.prototype.setContext,
  7. "Marionette:SetScreenOrientation": GeckoDriver.prototype.setScreenOrientation,
  8. "Marionette:SingleTap": GeckoDriver.prototype.singleTap,

// Addon service

  1. "Addon:Install": GeckoDriver.prototype.installAddon,
  2. "Addon:Uninstall": GeckoDriver.prototype.uninstallAddon,

// L10n service

  1. "L10n:LocalizeEntity": GeckoDriver.prototype.localizeEntity,
  2. "L10n:LocalizeProperty": GeckoDriver.prototype.localizeProperty,

// Reftest service

  1. "reftest:setup": GeckoDriver.prototype.setupReftest,
  2. "reftest:run": GeckoDriver.prototype.runReftest,
  3. "reftest:teardown": GeckoDriver.prototype.teardownReftest,

// WebDriver service

  1. "WebDriver:AcceptAlert": GeckoDriver.prototype.acceptDialog,

// deprecated, no longer used since the geckodriver 0.30.0 release
1. [ ] "WebDriver:AcceptDialog": GeckoDriver.prototype.acceptDialog,

  1. "WebDriver:AddCookie": GeckoDriver.prototype.addCookie,
  2. "WebDriver:Back": GeckoDriver.prototype.goBack,
  3. "WebDriver:CloseChromeWindow": GeckoDriver.prototype.closeChromeWindow,
  4. "WebDriver:CloseWindow": GeckoDriver.prototype.close,
  5. "WebDriver:DeleteAllCookies": GeckoDriver.prototype.deleteAllCookies,
  6. "WebDriver:DeleteCookie": GeckoDriver.prototype.deleteCookie,
  7. "WebDriver:DeleteSession": GeckoDriver.prototype.deleteSession,
  8. "WebDriver:DismissAlert": GeckoDriver.prototype.dismissDialog,
  9. "WebDriver:ElementClear": GeckoDriver.prototype.clearElement,
  10. "WebDriver:ElementClick": GeckoDriver.prototype.clickElement,
  11. "WebDriver:ElementSendKeys": GeckoDriver.prototype.sendKeysToElement,
  12. "WebDriver:ExecuteAsyncScript": GeckoDriver.prototype.executeAsyncScript,
  13. "WebDriver:ExecuteScript": GeckoDriver.prototype.executeScript,
  14. "WebDriver:FindElement": GeckoDriver.prototype.findElement,
  15. "WebDriver:FindElements": GeckoDriver.prototype.findElements,
  16. "WebDriver:Forward": GeckoDriver.prototype.goForward,
  17. "WebDriver:FullscreenWindow": GeckoDriver.prototype.fullscreenWindow,
  18. "WebDriver:GetActiveElement": GeckoDriver.prototype.getActiveElement,
  19. "WebDriver:GetAlertText": GeckoDriver.prototype.getTextFromDialog,
  20. "WebDriver:GetCapabilities": GeckoDriver.prototype.getSessionCapabilities,
  21. "WebDriver:GetCookies": GeckoDriver.prototype.getCookies,
  22. "WebDriver:GetCurrentURL": GeckoDriver.prototype.getCurrentUrl,
  23. "WebDriver:GetElementAttribute": GeckoDriver.prototype.getElementAttribute,
  24. "WebDriver:GetElementCSSValue": GeckoDriver.prototype.getElementValueOfCssProperty,
  25. "WebDriver:GetElementProperty": GeckoDriver.prototype.getElementProperty,
  26. "WebDriver:GetElementRect": GeckoDriver.prototype.getElementRect,
  27. "WebDriver:GetElementTagName": GeckoDriver.prototype.getElementTagName,
  28. "WebDriver:GetElementText": GeckoDriver.prototype.getElementText,
  29. "WebDriver:GetPageSource": GeckoDriver.prototype.getPageSource,
  30. "WebDriver:GetShadowRoot": GeckoDriver.prototype.getShadowRoot,
  31. "WebDriver:GetTimeouts": GeckoDriver.prototype.getTimeouts,
  32. "WebDriver:GetTitle": GeckoDriver.prototype.getTitle,
  33. "WebDriver:GetWindowHandle": GeckoDriver.prototype.getWindowHandle,
  34. "WebDriver:GetWindowHandles": GeckoDriver.prototype.getWindowHandles,
  35. "WebDriver:GetWindowRect": GeckoDriver.prototype.getWindowRect,
  36. "WebDriver:IsElementDisplayed": GeckoDriver.prototype.isElementDisplayed,
  37. "WebDriver:IsElementEnabled": GeckoDriver.prototype.isElementEnabled,
  38. "WebDriver:IsElementSelected": GeckoDriver.prototype.isElementSelected,
  39. "WebDriver:MinimizeWindow": GeckoDriver.prototype.minimizeWindow,
  40. "WebDriver:MaximizeWindow": GeckoDriver.prototype.maximizeWindow,
  41. "WebDriver:Navigate": GeckoDriver.prototype.navigateTo,
  42. "WebDriver:NewSession": GeckoDriver.prototype.newSession,
  43. "WebDriver:NewWindow": GeckoDriver.prototype.newWindow,
  44. "WebDriver:PerformActions": GeckoDriver.prototype.performActions,
  45. "WebDriver:Print": GeckoDriver.prototype.print,
  46. "WebDriver:Refresh": GeckoDriver.prototype.refresh,
  47. "WebDriver:ReleaseActions": GeckoDriver.prototype.releaseActions,
  48. "WebDriver:SendAlertText": GeckoDriver.prototype.sendKeysToDialog,
  49. "WebDriver:SetPermission": GeckoDriver.prototype.setPermission,
  50. "WebDriver:SetTimeouts": GeckoDriver.prototype.setTimeouts,
  51. "WebDriver:SetWindowRect": GeckoDriver.prototype.setWindowRect,
  52. "WebDriver:SwitchToFrame": GeckoDriver.prototype.switchToFrame,
  53. "WebDriver:SwitchToParentFrame": GeckoDriver.prototype.switchToParentFrame,
  54. "WebDriver:SwitchToWindow": GeckoDriver.prototype.switchToWindow,
  55. "WebDriver:TakeScreenshot": GeckoDriver.prototype.takeScreenshot,

cf. https://searchfox.org/mozilla-central/source/testing/marionette/driver.js https://searchfox.org/mozilla-central/source/remote/marionette/driver.js

Accessing Frames doesn't work

I can't access and execute javascript code in frames
`package main

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"time"

"github.com/njasm/marionette_client"

)

func main() {
client := marionette_client.NewClient()
client.Connect("", 0)
client.NewSession("", nil)

client.Navigate("http://docs.oracle.com/javase/8/docs/api/index.html")
client.SetContext(marionette_client.Context(marionette_client.CONTENT))
waittime := time.Duration(4) * time.Second
time.Sleep(waittime)
HTMLSource(client)

}

func gethtml(client *marionette_client.Client) string {
script := "return document.documentElement.outerHTML;"

args := make([]interface{}, 0, 0) // arguments to be passed to the function

var timeout uint
timeout = 1000   // milliseconds
sandbox := false // new Sandbox

r, err := client.ExecuteScript(script, args, timeout, sandbox)
if err == nil {
	return getJsonValue(r.Value)
} else {
	return ""
}

}

func getJsonValue(str string) string {
var d map[string]string
err := json.Unmarshal([]byte(str), &d)
if err != nil {
log.Println(err)
}
return d["value"]
}

func getscreenshot(client *marionette_client.Client) {
client.MaximizeWindow()

imgjson, err := client.Screenshot()
if err == nil {
	base64txt := getJsonValue(imgjson)
	data, err := base64.StdEncoding.DecodeString(base64txt)
	if err == nil {
		err = ioutil.WriteFile("screenshot.png", data, 0644)
		if err != nil {
			log.Println(err)
		}

	}

}

}

func HTMLSource(client *marionette_client.Client) {
//retval := ""
err := client.SwitchToParentFrame()
if err == nil {
fmt.Println(gethtml(client))
frames, err := client.FindElements(marionette_client.By(marionette_client.TAG_NAME), "frame")
if err == nil {
fmt.Println("FRAMES")
framenames := make([]string, 0, 3)
for _, frame := range frames {
framenames = append(framenames, frame.Attribute("name"))
}

		for i, framename := range framenames {
			fmt.Printf("FRAME %d %s\r\n", i, framename)

			err = client.SwitchToFrame(marionette_client.By(marionette_client.NAME), framename)
			//err = client.SwitchToFrame(marionette_client.By(marionette_client.ID), frame.Id())
			if err == nil {
				fmt.Println("NOERROR")
				fmt.Println(gethtml(client))
			} else {
				fmt.Println("ERROR")
				fmt.Println(err)
			}
		}
	} else {
		fmt.Println(err)
	}

} else {
	fmt.Println(err)
}

}
`
It works for the first frame only (I call frame 0)
but not on the others. This is an excerpts of the output:

FRAME 1 packageFrame BY name map[using:name value:packageFrame]ERROR Unable to locate element: packageFrame FRAME 2 classFrame BY name map[using:name value:classFrame]ERROR Unable to locate element: classFrame
Why it doesn't work. There is a mistake on my part? It's an error on marionette_client or on Firefox.
I trie Firefov 52,2esr and 54. Do you have the same problem?

Implement Firefox process start/management

when NewSession is called, we should check if a firefox Instance is already running and try to connect to it, else we should try to find and start the processa and manage it. when ending a session we should also end the process if it was started by us.

Opening more than one tab and get a pointer to everyone of them

I wanted to open more than one tab on firefox via marionette_client, but I need a reference, a pointer to them. My aim is to open for example 10 tabs in parallel with 10 different urls and after they are loaded get for example their html source. Is it possible with marionette_client?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.