phimage/Erik

doc.toHTML only gives last few lines of html

Closed this issue · 3 comments

When I print out the doc it only has the last few lines of html code. I have tried doc.body and it works fine. So I am wondering is it the problem of Erik?
p.s. This also happened to the google website. I used the code in the example.

The print statement gives me following:

script>

<script src="/tritON/scripts/bootstrap.js"></script>

<script src="https://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script>




</body>
</html>

My code is below:

Erik.visit(url: url) { obj, err in
    if let e = err {
        print("Error: \(e)")
    } else if let doc = obj {
        print(doc.toHTML!)
        if let input = doc.querySelectorAll("input[id='ssousername']").first {
            input["value"] = user
        }
        if let input = doc.querySelectorAll("input[id='ssopassword']").first {
            input["value"] = password
        }
        if let form = doc.querySelector("form[id='login']") as? Form {
            form.submit()
        }
    }
}

It a little difficult to know when the page is fully loaded, because of javascript.
So I have implemented 3 ways,
as enum isLoading, estimatedProgress, navigationDelegate

The one I use in unit test is the last one. Maybe I will add it as default one

You can try it

let engine = Erik.sharedInstance.layoutEngine as? WebKitLayoutEngine
engine?.pageLoadedPolicy = .navigationDelegate

What is the use of navigationDelegate? I also find it a problem that I cannot tell when is the page is fully loaded. Now I use asyncAfter to wait a few seconds to ensure that the page is fully loaded. Is there a better way to do it?

What is the use of navigationDelegate?

https://github.com/phimage/Erik/blob/master/Sources/LayoutEngine.swift#L74
To known if a page is loaded, I use one of the three condition on WebKit

For .navigationDelegate I wait this event (webViewWebContentProcessDidTerminate)
https://github.com/phimage/Erik/blob/master/Sources/LayoutEngine.swift#L195

I also find it a problem that I cannot tell when is the page is fully loaded. Now I use asyncAfter to wait a few seconds to ensure that the page is fully loaded. Is there a better way to do it?

It depends on the web site. If you find a solution for your needs, it's ok
Some times I wait for a specific condition like an html node appears

You can also inject javascript and wait response