Full Screen Source Access at WatiN

I am looking for a way to submit my web pages through the DTD validator as part of my WatiN tests, but I have not yet found a clean way to access raw HTML. Is there a built-in way to do this?

I think I could get access to the properties IE.InternetExplorerand QueryInterfacethe interface IPersistStreamInitand serialize the document IStream, but it looks like a lot of work for that, I think there should be a fairly common task.

Did I miss something obvious at WatiN? Or can someone think of a better solution than the one I outlined above? This solution is highly dependent on IE.

+3
source share
5 answers

Here's how you access the source code:

browser.ActiveElement.Parent.OuterHtml
+1

string html = browser.Body.Parent.OuterHtml;

+1

, . Watfor. Sourceforge.

0

, , , HTML- - WatiN , , WatiN - , .

, , . , .

    private static TextVariant GetWebPageSource(IE browser)
    {
    IHTMLDocument2 htmlDocument = ((IEDocument)(browser.DomContainer.NativeDocument)).HtmlDocument;
    Encoding encoding = Encoding.GetEncoding(htmlDocument.charset);
        IPersistStreamInit persistStream = (IPersistStreamInit)htmlDocument;
        MinimalIStream stream = new MinimalIStream();
        persistStream.Save(stream, false);
        return new TextVariant(encoding.GetString(stream.ToArray()));
    }

    [Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPersistStreamInit
    {
        void GetClassID(out Guid pClassID);
        int IsDirty();
        void Load(IStream pStm);
        void Save(IStream pStm, bool fClearDirty);
        void GetSizeMax(out long pcbSize);
        void InitNew();
    }

    // http://stackoverflow.com/questions/6601355/passing-an-fstream-or-equivalent-from-c-to-c-through-cli
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class MinimalIStream : MemoryStream, IStream
    {
        public MinimalIStream() { }

        public MinimalIStream(byte[] data) : base(data) { }

        #region IStream Members
        public void Write(byte[] pv, int cb, IntPtr pcbWritten)
        {
            base.Write(pv, 0, cb);
            if (pcbWritten != IntPtr.Zero)
                Marshal.WriteInt64(pcbWritten, (long)cb);
        }

        public void Stat(out STATSTG pstatstg, int grfStatFlag)
        {
            pstatstg = new STATSTG();
            pstatstg.cbSize = base.Length;
        }

        public void Read(byte[] pv, int cb, IntPtr pcbRead)
        {
            long bytes_read = base.Read(pv, 0, cb);
            if (pcbRead != IntPtr.Zero) Marshal.WriteInt64(pcbRead, bytes_read);
        }

        public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
        {
            long pos = base.Seek(dlibMove, (SeekOrigin)dwOrigin);
            if (plibNewPosition != IntPtr.Zero) Marshal.WriteInt64(plibNewPosition, pos);
        }

        public void Clone(out IStream ppstm)
        {
            ppstm = null;
        }

        public void Commit(int grfCommitFlags)
        {
        }

        public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
        }

        public void LockRegion(long libOffset, long cb, int dwLockType)
        {
        }

        public void SetSize(long libNewSize)
        {
        }

        public void Revert()
        {
        }

        public void UnlockRegion(long libOffset, long cb, int dwLockType)
        {
        }
        #endregion
    }
0

:

browser.ActiveElement.Parent.OuterHtml

, "ActiveElement", :

browser.Body.Parent.OuterHtml

, . (browser IE)

, , , DOM . URL ( WatiN), .

0

Source: https://habr.com/ru/post/1717473/


All Articles