callgraph/sharelatex/client.go

117 lines
2.5 KiB
Go

package sharelatex
import (
"fmt"
"math/rand"
"net/http"
"os"
"path/filepath"
"time"
"github.com/tebeka/selenium"
)
type driver struct {
wd selenium.WebDriver
retries uint
}
func (d driver) FindXpath(selector string) selenium.WebElement {
return d.Find(selenium.ByXPATH, selector)
}
func (d driver) FindByCSS(selector string) selenium.WebElement {
return d.Find(selenium.ByCSSSelector, selector)
}
func (d driver) FindByName(selector string) selenium.WebElement {
return d.Find(selenium.ByName, selector)
}
func (d driver) FindByText(selector string) selenium.WebElement {
return d.Find(selenium.ByLinkText, selector)
}
func (d driver) Find(by, selector string) selenium.WebElement {
for i := uint(0); i < d.retries; i++ {
e, err := d.wd.FindElement(by, selector)
if err == nil {
return e
}
time.Sleep(time.Millisecond * 100)
}
fmt.Fprintf(os.Stderr, "elemenet %s not found (%s)", selector, by)
os.Exit(1)
return nil
}
func IntegrationTest(url string) error {
_, err := http.Get(url)
if err != nil {
fmt.Printf("failed to get '%s': %s", url, err)
}
caps := selenium.Capabilities{"browserName": "firefox"}
wd, err := selenium.NewRemote(caps, "")
if err != nil {
return err
}
defer wd.Quit()
d := driver{wd, 100}
// Get simple playground interface
fmt.Fprintf(os.Stderr, "visit url %s\n", url)
wd.Get(url)
e := d.FindXpath("//input[@type='email']")
e.Clear()
e.SendKeys("user1@higgsboson.tk")
e = d.FindXpath("//input[@type='password']")
e.Clear()
e.SendKeys("password")
e = d.FindXpath("//button[@type='submit']")
e.Click()
d.FindByText("Account")
e = d.FindByText("New Project")
e.Click()
e = d.FindByText("Blank Project")
e.Click()
e = d.FindXpath("//input[@type='text']")
e.Clear()
e.SendKeys(fmt.Sprintf("p %d", rand.Int63()))
e = d.FindByCSS("button.btn-primary") // Create
e.Click()
e = d.FindXpath("//a[@ng-controller='ShareController']") // Share
e.Click()
e = d.FindByCSS("button.btn-primary") // Close
e.Click()
e = d.FindByCSS(`i.fa-comment`)
e.Click()
e = d.FindByCSS(`div.new-message textarea`)
e.SendKeys("hello world" + selenium.EnterKey)
e = d.FindByCSS("i.fa.fa-upload") // Upload
e.Click()
e = d.FindByName("file") // Create
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
e.SendKeys(filepath.Join(dir, "./tech-support.jpg"))
e = d.FindByCSS("textarea") // Latex Doc
e.Clear()
e.SendKeys(`\documentclass{article} \begin{document} ello \end{document}`)
time.Sleep(time.Millisecond * 1000)
return nil
}