sclevine / agouti

A WebDriver client and acceptance testing library for Go
MIT License
821 stars 102 forks source link

failed to retrieve popup text: request unsuccessful - for phantomjs #158

Open turkogluc opened 6 years ago

turkogluc commented 6 years ago

index.html

<html>
<body>
<script>alert('cemal');</script>
</body>
</html>

main.go

func main() {
    driver := agouti.PhantomJS()

    capabilities := agouti.NewCapabilities().Browser("firefox").With("handlesAlerts")
    driver.Open(capabilities)

    if err := driver.Start(); err != nil {
        log.Fatal("Failed to start driver:", err)
    }

    page, err := driver.NewPage()
    if err != nil {
        log.Fatal("Failed to open page:", err)
    }

    if err := page.Navigate("http://localhost/index.html"); err != nil {
        log.Fatal("Failed to navigate:", err)
    }

    sectionTitle, err := page.HTML()
    log.Println(sectionTitle)

    //log.Println(page.Find("#alert").Text())

    popup,err := page.PopupText()
    if err != nil {
        log.Fatal("No pop up:", err)
    }else{
        log.Println(popup)
    }

    if err := driver.Stop(); err != nil {
        log.Fatal("Failed to close pages and stop WebDriver:", err)
    }
}

I am trying to get pop up text. I tried alert,prompt functions but result does not change. I always get following error:

2018/04/03 00:12:53 No pop up:failed to retrieve popup text: request unsuccessful: Invalid Command Method - {"headers":{"Accept-Encoding":"gzip","Host":"127.0.0.1:34161","User-Agent":"Go-http-client/1.1"},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{"anchor":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"/alert_text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/alert_text","queryKey":{},"chunks":["alert_text"]},"urlOriginal":"/session/9b5c8930-36ba-11e8-b985-4b21cd526507/alert_text"}
exit status 1

I dont know if I am doing something wrong or function does not work properly ?

sclevine commented 6 years ago

PhantomJS doesn't support alert boxes.

Also, PhantomJS does not appear to be maintained any longer, so you might consider switching to headless chrome: https://github.com/ariya/phantomjs/issues/15361

turkogluc commented 6 years ago

Thanks for that quick answer. I was already trying chrome headless but apparently doing something wrong again.

func DriverInit()(err error){
    driver = agouti.ChromeDriver(agouti.ChromeOptions("args", []string{"--headless", "--disable-gpu", "--no-sandbox","--disable-xss-auditor"}))

    mutex = &sync.Mutex{}

    err = nil
    if err = driver.Start(); err != nil {
        log.Fatal("Failed to start driver:", err)
    }

    page, err = driver.NewPage(agouti.Browser("chrome"))
    if err != nil {
        log.Fatal("Failed to open page:", err)
    }
    return err
}

func BrowserTest(url string,payload string)(bool) {
    mutex.Lock()
    //time.Sleep(time.Millisecond)

    if err := page.Navigate(url); err != nil {
        log.Fatal("Failed to navigate:", err)
    }

    //sectionTitle, err := page.HTML()
    //log.Println("file:", sectionTitle)
    //
    ////log.Println(page.Find("#alert").Text())
    //
    //u,_ := page.URL()
    //log.Print("url",u)
    state := false
    popup,err := page.PopupText()
    if err != nil {
        log.Print("No pop up:", err)
        state = false
    }else{
        page.ConfirmPopup()
        log.Println("yes ", popup)
        state = true
    }s
    mutex.Unlock()
    return state
} 

The result gives me around %10 percent false positives. I dont get it why it returns true if there is no alert box.

Also When I am trying to use some functions such as Page.Url() or Page.Html() it returns empty string. What could be the reason

sclevine commented 6 years ago

Web browsers interactions aren't synchronous. Just like when you interact with a web browser manually, actions like navigating to a URL or clicking take time. For your specific example, you aren't waiting for the page to load. This can be accomplished by waiting for the popup to appear (using Go code or Gomega, see agouti.org for examples) or by setting a non-zero page load timeout.

Also, it looks like you're ignoring errors in both cases that you receive an empty string.